Android蓝牙连接到配对设备

时间:2015-01-19 20:07:48

标签: java android bluetooth

目前我有一个设备(Arduino的蓝牙模块),我想通过蓝牙连接。但每次我尝试连接时,都没有任何反应。有人可以告诉我我做错了什么吗? 我的代码:

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();
       }
    }
}

最终我想连接到设备,然后创建一个套接字,然后我可以读取和写入字节。感谢

2 个答案:

答案 0 :(得分:1)

您忘记在connect()上致电BluetoothSocket了:

// ...
BluetoothSocket bs = bd.createInsecureRfcommSocketToServiceRecord(CONNUUID);
bs.connect(); // note: blocking call
// ...

请参阅example code on Google Developer pages

答案 1 :(得分:0)

如果您的设备已配对。然后首先获取设备UUID

final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);

// now tm.getdeviceID()...and is this equal to your CONNUUID?

在清单中设置蓝牙权限...现在通过以下方式查看设备列表:

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();   

Set<BluetoothDevice> devices = adapter.getBondedDevices();    

for (BluetoothDevice device : devices) {

   String sDeviceName = device.getName().trim();

   Log.d("device_found", sDeviceName);
}