currently I have a device (a bluetooth module for an Arduino) that I would like to connect to via bluetooth. But every time I try to connect, nothing happens. Can someone please tell me what I am doing wrong? My Code:
private static final UUID CONNUUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");
public void connectDevice(BluetoothDevice bd){
try{
pairDia = ProgressDialog.show(this, "", "Connecting...", true, true);
BluetoothSocket bs = bd.createInsecureRfcommSocketToServiceRecord(CONNUUID);
}catch(Exception e){
e.printStackTrace();
this.finish();
}
}
}
Ultimately I would like to connect to the device, then create a socket to it which I can then read and write bytes to. Thanks
You're forgetting to call connect()
on your BluetoothSocket
:
// ...
BluetoothSocket bs = bd.createInsecureRfcommSocketToServiceRecord(CONNUUID);
bs.connect(); // note: blocking call
// ...
If your device is paired. Then get the device UUID
first with this:
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
// now tm.getdeviceID()...and is this equal to your CONNUUID?
Set the bluetooth permission... in manifest.. and now see the list of devices by the following:
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
String sDeviceName = device.getName().trim();
Log.d("device_found", sDeviceName);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.