Android:如何连接隐藏的蓝牙设备?

时间:2014-11-05 07:40:35

标签: java android

我有一个隐藏的蓝牙设备,但我知道它的蓝牙MAC地址。

如何使用Android连接到该设备?

2 个答案:

答案 0 :(得分:0)

来自此处的文档:http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html

  

要获取BluetoothDevice,请使用BluetoothAdapter.getRemoteDevice(String)   创建一个代表已知MAC地址的设备

在您刚刚连接到它之后就像通过设备发现检索它一样。

答案 1 :(得分:0)

我是这样做的:

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("Some_MAC");
        BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            mBluetoothAdapter.cancelDiscovery();
            Method getUuidsMethod = BluetoothAdapter.class.getDeclaredMethod("getUuids", null);
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
            int sdk = Integer.parseInt(Build.VERSION.SDK);
            if(sdk >= 10){
                //sdk 2.3?? java.io.IOException: Connection refused
                tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
            }else {
                tmp = device.createRfcommSocketToServiceRecord(uuid);
            }
            Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
            tmp = (BluetoothSocket) m.invoke(device, 1);
            tmp.connect();
} catch (IOException e) {
            Log.e(TAG, "failed: ", e);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }