蓝牙配对谷歌眼镜

时间:2014-08-05 11:30:47

标签: android bluetooth google-glass google-gdk

使用Google Glass,我能够发现蓝牙设备并查看其地址和信息。但是,我无法让Glass与它们配对(粘合)。

更新

按照this page上的说明,我现在正试图获得绑定,但出于某种原因,BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)永远不会发生。

private void pairDevice(BluetoothDevice Ddevice) {
    Log.d("MY_LOG", "Try to pair " + Ddevice.getName());
    try{
        Method m = Ddevice.getClass().getMethod("createBond", (Class[]) null);
        m.invoke(Ddevice, (Object[]) null);
        Log.d("MY_LOG", "Pairing " + Ddevice.getName());
    }catch(Exception e){
        Log.d("MY_LOG", "Error: ");
        e.printStackTrace();
    }
}

在LOG中,我总是得到“Pairing DeviceName”,但是当我搜索绑定的设备时,它仍然是空的。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

所以我会回答自己的问题,因为我找到了方法。

首先,设备的发现非常简单,我使用onCreate()(除了你需要的所有其他类型的代码):

MyBT = BluetoothAdapter.getDefaultAdapter();
MyBT.startDiscovery();
Filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);                    // Register the BroadcastReceiver
Filter2 = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);         // Register the Bond changing state
registerReceiver(mReceiver, Filter);                                        // Don't forget to unregister during onDestroy
registerReceiver(mReceiver, Filter2);                                       // ******

然后在BroadcastReceiver您需要管理设备和配对请求:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {           // Create a BroadcastReceiver for ACTION_FOUND
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {                      // When discovery finds a device
            BluetoothDevice BTdevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Get the BluetoothDevice object from the Intent
            ListDev.add(BTdevice);                                              // Add the device to an array adapter to show...
        }     
        if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
            BluetoothDevice device = ListDev.get(selectedDevice);
            byte[] pinBytes = getStrFromName(device.getName(),7,11).getBytes();  // My devices had their own pin in their name, you can put a constant pin here...  
            try {
                Log.d("MY_LOG", "Try to set the PIN");
                Method m = device.getClass().getMethod("setPin", byte[].class);
                m.invoke(device, pinBytes);
                Log.d("MY_LOG", "Success to add the PIN.");
                try {
                    device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
                    Log.d("MY_LOG", "Success to setPairingConfirmation.");
                } catch (Exception e) {
                    Log.e("MY_LOG", e.getMessage());
                    e.printStackTrace();
                } 

            } catch (Exception e) {
                Log.e("MY_LOG", e.getMessage());
                e.printStackTrace();
            }
        }
    }
}; 

之后设备已绑定,您可以像在Android webpage example中一样管理与UUID和套接字的连接。

希望这有帮助!