gpt4 book ai didi

android - 蓝牙配对谷歌眼镜

转载 作者:行者123 更新时间:2023-11-30 02:45:20 25 4
gpt4 key购买 nike

使用谷歌眼镜,我能够发现蓝牙设备并查看它们的地址和信息。但是,我无法让 Glass 与它们配对(绑定(bind))。

更新

按照 this page 上的说明进行操作现在我正在尝试建立绑定(bind),但由于某种原因,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();
}
}

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

任何帮助将不胜感激。

最佳答案

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

首先,设备的发现非常简单,在我使用的 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();
}
}
}
};

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

希望这对您有所帮助!

关于android - 蓝牙配对谷歌眼镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25137726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com