Problem
Toss:
"Record" writing Bluetooth module drivers and underlying Hart devices in Android
Period, refer to:
Bluetooth | Android developers–managingaconnection
Refer to "Connecting as a client" in:
TMP = Device.createrfcommsockettoservicerecord (MY_UUID);
Encounter a problem that the UUID does not understand.
Then go anywhere.
Http://www.guidgenerator.com/online-guid-generator.aspx
Get a UUID:
E214d9ae-c3ba-4e25-abb5-299041353bc3
The result runs to:
try { //Connect the device through the socket. This would block //until it succeeds or throws an exception mmsocket.connect (); } catch (IOException ConnectEx ception) { //Unable to connect, close the socket and get out try { mmsocket.close (); } catch (Ioexcepti On closeexception) {} return; }
In the following:
When you throw an exception.
That
Encounter Createrfcommsockettoservicerecord UUID do not understand, and Bluetoothsocket connect failed.
"Resolution Process"
1. Reference:
Android–why can ' t HTC Droid running OTA 2.1 communicate with RFCOMM? –stack Overflow
To try:
Method m = Device.getclass (). GetMethod ("Createrfcommsocket", new class[] {int.class}); mbtsocket = (Bluetoothsocket) M.invoke (device, 1);
The result is not very right to look at. I'm not going to try anymore.
2. Reference:
Need Bluetooth UUID Clarification–google Groups
To try:
00001101-0000-1000-8000-00805f9b34fb
The result is directly hung out.
3. Try again:
00000003-0000-1000-8000-00805f9b34fb
will also hang.
4. Then go to search:
Android Bluetooth Connect fail
Then go to reference:
Android Bluetooth can ' t connect–stack Overflow
And:
The Missing manual:android Bluetooth rfcomm«wires is Obsolete
To try, use the code:
Create a broadcastreceiver for action_found private final broadcastreceiver mreceiver = new Broadcastreceiver () { P ublic void OnReceive (context context, Intent Intent) {//Bluetoothdevice Deviceextra = Intent.getparcelableextra (" Android.bluetooth.device.extra.DEVICE ");//parcelable[] Uuidextra = Intent.getparcelablearrayextra (" Android.bluetooth.device.extra.UUID "); String action = Intent.getaction (); When Discovery finds a device if (BluetoothDevice.ACTION_FOUND.equals (ACTION)) { //Get the Bluetoothdevice ob Ject from the Intent bluetoothdevice Btdev = Intent.getparcelableextra (bluetoothdevice.extra_device); parcelable[] Btdevuuid = Intent.getparcelablearrayextra (Bluetoothdevice.extra_uuid);
The btdevuuid that were debugged are null.
5. Swap:
if (BluetoothDevice.ACTION_FOUND.equals (ACTION)) { //Get The Bluetoothdevice object from the Intent Bluetoothdevice Btdev = Intent.getparcelableextra (bluetoothdevice.extra_device); parcelable[] Btdevuuid = Intent.getparcelablearrayextra (bluetoothdevice.extra_uuid); UUID Btdevuuid = Intent.getparcelableextra (Bluetoothdevice.extra_uuid);
It's still not working.
6. Reference:
Issue 15919–android–bluetooth Connect fails under 2.3.3 for SPP devices using secure comms
Mentioned in the following:
Bluetoothdevice | Android Developers
In the prompt:
Hint:if connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-8000-008 05F9B34FB. However if you is connecting to a Android peer then please generate your own unique UUID. |
So try this UUID again:
00001101-0000-1000-8000-00805f9b34fb
In the end it is possible to:
The corresponding relevant code is:
Private String Mactekhartmodemname; Private UUID Mactekhartmodemuuid; void Afterfoundbthartmodem (Bluetoothdevice btdev, parcelable[] btdevuuid) {void Afterfoundbthartmodem ( Bluetoothdevice Btdev, UUID btdevuuid) {if (null! = Btdevuuid) {}//mactekhartmodemname = Btdev.getname (); "Mactekviator75fe"//mactekhartmodemuuid = uuid.fromstring (mactekhartmodemname); String Uuidvalue; Http://www.guidgenerator.com/online-guid-generator.aspx//uuidvalue = "E214D9AE-C3BA-4E25-ABB5-299041353BC3"; Https://groups.google.com/forum/#!topic/android-developers/vyTEJOXELos//uuidvalue = "00001101-0000-100 0-8000-00805F9B34FB "; Uuidvalue = "00000003-0000-1000-8000-00805F9B34FB"; Uuidvalue = "00001101-0000-1000-8000-00805F9B34FB"; Mactekhartmodemuuid = uuid.fromstring (Uuidvalue); Connectthread connectbtthread = new Connectthread (Btdev); Connectbtthread.start (); } private class Connectthread extends Thread {private final bluetoothsocket mmsocket; Private final Bluetoothdevice Mmdevice; Public Connectthread (Bluetoothdevice device) {//Use a temporary object that's later assigned to Mmsocket, Because Mmsocket is final bluetoothsocket tmp = NULL; Mmdevice = device; Get a bluetoothsocket to connect with the given Bluetoothdevice try {//My_uuid is the app ' s UUID string, also used by the server code TMP = Device.createrfcommsockettoservicerecord (mactekhartmodemuu ID);//00001101-0000-1000-8000-00805F9B34FB} catch (IOException e) {} mmsocket = tmp; } public void Run () {//Cancel discovery because it would slow down the connection Mblue Toothadapter.canceldiscovery (); try {//Connect the device through the socket. This would block//until it succeeds or throws an exception Mmsocket.conNect (); } catch (IOException connectexception) {//Unable to connect; Close the socket and get out try {mmsocket.close (); } catch (IOException closeexception) {} return; }//Do work to manage the connection (in a separate thread) Manageconnectedsocket (Mmsocket); }/** would cancel an in-progress connection, and close the socket */public void Cancel () { try {mmsocket.close (); } catch (IOException e) {}}}
Summary
Here, you must use the UUID of the Android SSP (protocol stack default):
00001101-0000-1000-8000-00805f9b34fb
To normal and external, but also the SSP serial port Bluetooth device to connect.
Createrfcommsockettoservicerecord's UUID problem and Bluetoothsocket's connect failure when connecting Bluetooth devices in Android