android蓝牙串口连接代码 在做android蓝牙串口连接的时候一般会使用
BluetoothSocket tmp = null;毕业论文
// Get a BluetoothSocket for a connection with the // given BluetoothDevice try { tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { Log.e(TAG, "create() failed", e); }
然后是tmp。connect方法
可是 BluetoothSocket 。connect方法本身就会报很多错误
这是我自己修改的方法
private class ConnectThread extends Thread { private BluetoothSocket mmSocket; private BluetoothDevice mmDevice; ImprovedBluetoothDevice improvedBluetoothDevice; public ConnectThread(BluetoothDevice device) { mmDevice = device; BluetoothSocket tmp = null;
improvedBluetoothDevice = new ImprovedBluetoothDevice(device); }
public void run() { Log.i(TAG, "BEGIN mConnectThread"); setName("ConnectThread");
// Always cancel discovery because it will slow down a connection mAdapter.cancelDiscovery();
String connectionType = "?"; //蓝牙设备有三十个端口号,是,从1到30,可以一个一个试,这个办法虽然笨,可是管用 for(int port = 1; port < 31; port++) { Log.d(TAG, "Connecting with port: " + port); try { connectionType = "Secure"; Log.d(TAG, "Attempting createRfcommSocket"); BluetoothSocket s = improvedBluetoothDevice.createRfcommSocket(port);
[1] [2] [3] [4] [5] [6] 下一页
|