PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 23 → Bluetooth example for Android
Bluetooth example for Android
Débuté par MichaelRug, 19 mai 2015 22:14 - 3 réponses
Posté le 19 mai 2015 - 22:14
Hello:
I was testing the Bluetooth example for Android (part of the Android_System example, and so far it works fine, but it only scans and detects Bluetooth devices.

I'm wondering if it's possible to connect to the devices and send/receive information. There are specific commands related to Bluetooth (like the BTConnect) but some are not available for Android (only for Windows)... Is there any way to do this?

Thanks
Michael
Posté le 21 mai 2015 - 10:44
Hello Michael,

I used the following method to send text to a Bluetooth Printer as WM19 does not have a Print to Bluetooth device function. I am not a specialist in Java and there may be better code to use but this worked for me.

// Used the

sListDevices = BTListDevice()

//to get Bluetooth devices

// Then extracted the Devices MAC addresses with the following:

FOR EACH STRING sTmpString OF sListDevices SEPARATED BY CR
// BluetoothDevices is a table to keep the device names
BluetoothDevices.Device_Number = Val(ExtractString(sTmpString,1))
BluetoothDevices.DevicesFound = ExtractString(sTmpString,1,TAB,FromEnd)
BluetoothDevices.DefaultDevice = False
BluetoothDevices.Device_Mac = ExtractString(sTmpString,2)

END

//In a button Code call Java Function
SendDataToPrinter(gsBluetoothPrinterMac,gsReceiptString)

// I tested a lot of code and this was the only Java code that worked for my app.
//Created a JAVA Procedure (SendDataToPrinter)

import android.content.Intent;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;

public static void SendDataToPrinter(String MacAddress, String Receipttext)
{
Context oContext = getApplicationContext();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,MacAddress+"|"+Receipttext);
sendIntent.setType("text/plain");
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
oContext.startActivity(sendIntent);

}

**************
Compiled into app
Above only sends the text to the Bluetooth device app
**************

//The manufacturers of Bluetooth devices normally have JAVA SDK.
//You will have to use something like Androidstudio to open that SDK code

//The passed string then needs to extract the mac and text string passed with something like the following

Intent intent = getIntent();
String Receipttext = (intent.getStringExtra(Intent.EXTRA_TEXT));
Receipttext = Receipttext.substring(18); //Takes the Mac details out

String MacAddress = (intent.getStringExtra(Intent.EXTRA_TEXT));
MacAddress = MacAddress.substring(0,17); //Get the mac address

con_dev = mService.getDevByMac(MacAddress);
mService.connect(con_dev); //Connect to device

///Compile into an app and install on android device
// The SDK from the Printer manufacturer takes care of printing to the printer
// You however do not get feedback from the second android app.
// It is however only text being printed. No graphical details

I hope this give you some direction.

Regards

Hendrik Labuschagne
Posté le 05 novembre 2015 - 00:42
hi bro.

can you explain how java procedure invoke the another app with intent

regards
Posté le 13 juin 2018 - 16:45
Bonjour Hendrik,

pouvez vous adapter le code afin qu'il permettre de se connecter à un périphérique bluetooth ?
Merci d'avance.