【問題】html
折騰:java
【記錄】編寫Android中的藍牙模塊驅動和底層HART設備android
期間,參考:app
Bluetooth | Android Developers – ManagingAConnectionsocket
參考「Connecting as a client」中的:ide
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
遇到UUID不懂的問題。wordpress
而後隨便去ui
http://www.guidgenerator.com/online-guid-generator.aspxgoogle
弄了個UUID:spa
e214d9ae-c3ba-4e25-abb5-299041353bc3
結果運行到:
try {
// Connect the device through the socket. This will 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;
}
中的:
mmSocket.connect();
時就拋異常了。
即:
遇到createRfcommSocketToServiceRecord的UUID不懂,以及BluetoothSocket的connect失敗。
【解決過程】
1.參考:
android – Why can’t HTC Droid running OTA 2.1 communicate with RFCOMM? – Stack Overflow
去試試:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
mBTSocket = (BluetoothSocket) m.invoke(device, 1);
結果看着就不太對啊。。就不繼續試了。
2.參考:
Need Bluetooth UUID clarification – Google Groups
去試試:
00001101-0000-1000-8000-00805F9B34FB
結果直接掛掉。
3.再去試試:
00000003-0000-1000-8000-00805F9B34FB
也會掛掉。
4.後來再去搜:
android bluetooth connect fail
而後去參考:
android bluetooth can’t connect – Stack Overflow
和:
The Missing Manual: Android Bluetooth RFCOMM « Wires Are Obsolete
去試試,用代碼:
// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public 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 object from the Intent
BluetoothDevice btDev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Parcelable[] btDevUuid = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
調試獲得的btDevUuid都是null的。
5.換用:
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);
仍是不行。
6.參考:
Issue 15919 – android – Bluetooth connect fails under 2.3.3 for SPP devices using secure comms
中提到的:
BluetoothDevice | Android Developers
中提示的:
Hint: If you are connecting to a Bluetooth serial board then try using the well-known SPP UUID 00001101-0000-1000-8000-00805F9B34FB. However if you are connecting to an Android peer then please generate your own unique UUID. |
因此再去試試這個UUID:
00001101-0000-1000-8000-00805F9B34FB
最終是能夠了:
對應的相關代碼爲:
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-1000-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 is 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(mactekHartModemUuid);//00001101-0000-1000-8000-00805F9B34FB
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will 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);
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
【總結】
此處,必須使用Android的SSP(協議棧默認)的UUID:
00001101-0000-1000-8000-00805F9B34FB
才能正常和外部的,也是SSP串口的藍牙設備去鏈接。