如何找到正确的 UUID? [英] How do I find the proper UUID?
问题描述
大约一周前,我从这里的某个人那里得到了帮助,但我的代码似乎仍然存在问题.我正在运行 Android 2.0,所以我无法使用这些方法来获取 UI,而是需要反射性地调用这些方法.下面是我的代码
I received help from someone here a week or so ago, but there seems to still be a problem with my code. I am running Android 2.0 so I cannot use the methods to get the UI, instead I need to call the methods reflectively. Below is my code
public ConnectThread(BluetoothDevice device, boolean secure) {
Log.d(TAG,"here5");
mmDevice = device;
BluetoothSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
Log.d(TAG,"here6");
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
ParcelUuid[] uuids = servicesFromDevice( mmDevice );
Log.d( Integer.toString( uuids.length ),"here7");
try {
if (secure) {
tmp = device.createRfcommSocketToServiceRecord(
uuids[0].getUuid());
} else {
tmp = device.createInsecureRfcommSocketToServiceRecord(
uuids[0].getUuid());
}
我在上面的最后一行代码中收到 NullPointerException.我假设它在谈论我的 uuids[0].getUuid() 调用.servicesFromDevice 函数在下面...
I receive a NullPointerException in the very last line of code above. I'm assuming it is talking about my uuids[0].getUuid() call. The servicesFromDevice function is below...
public ParcelUuid[] servicesFromDevice(BluetoothDevice device) {
try {
Class cl = Class.forName("android.bluetooth.BluetoothDevice");
Class[] par = {};
Method method = cl.getMethod("getUuids", par);
Object[] args = {};
ParcelUuid[] retval = (ParcelUuid[]) method.invoke(device, args);
return retval;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
这是我的 LogCat
Here is my LogCat
06-19 13:11:07.002: D/MainMenu(13137): onActivityResult -1
06-19 13:11:07.002: D/BluetoothService(13137): connect to: 00:06:66:00:A2:11
06-19 13:11:07.002: D/BluetoothService(13137): here1
06-19 13:11:07.002: D/BluetoothService(13137): here2
06-19 13:11:07.002: D/BluetoothService(13137): here5
06-19 13:11:07.010: D/BluetoothService(13137): here6
06-19 13:11:07.017: D/AndroidRuntime(13137): Shutting down VM
06-19 13:11:07.017: W/dalvikvm(13137): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-19 13:11:07.025: E/AndroidRuntime(13137): FATAL EXCEPTION: main
06-19 13:11:07.025: E/AndroidRuntime(13137): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {my.eti.commander/my.eti.commander.MainMenu}: java.lang.NullPointerException
06-19 13:11:07.025: E/AndroidRuntime(13137): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
06-19 13:11:07.025: E/AndroidRuntime(13137): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
06-19 13:11:07.025: E/AndroidRuntime(13137): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
06-19 13:11:07.025: E/AndroidRuntime(13137): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
06-19 13:11:07.025: E/AndroidRuntime(13137): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 13:11:07.025: E/AndroidRuntime(13137): at android.os.Looper.loop(Looper.java:130)
06-19 13:11:07.025: E/AndroidRuntime(13137): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-19 13:11:07.025: E/AndroidRuntime(13137): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 13:11:07.025: E/AndroidRuntime(13137): at java.lang.reflect.Method.invoke(Method.java:507)
06-19 13:11:07.025: E/AndroidRuntime(13137): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-19 13:11:07.025: E/AndroidRuntime(13137): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-19 13:11:07.025: E/AndroidRuntime(13137): at dalvik.system.NativeStart.main(Native Method)
06-19 13:11:07.025: E/AndroidRuntime(13137): Caused by: java.lang.NullPointerException
06-19 13:11:07.025: E/AndroidRuntime(13137): at my.eti.commander.BluetoothService$ConnectThread.<init>(BluetoothService.java:409)
06-19 13:11:07.025: E/AndroidRuntime(13137): at my.eti.commander.BluetoothService.connect(BluetoothService.java:180)
06-19 13:11:07.025: E/AndroidRuntime(13137): at my.eti.commander.MainMenu.onActivityResult(MainMenu.java:238)
06-19 13:11:07.025: E/AndroidRuntime(13137): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
06-19 13:11:07.025: E/AndroidRuntime(13137): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
06-19 13:11:07.025: E/AndroidRuntime(13137): ... 11 more
推荐答案
来自@Devunwired 以上评论区......
From @Devunwired in the comment section above......
您是否尝试过使用许多嵌入式设备使用的common SPP ID
,而不是尝试查询设备?
Have you tried using the common SPP ID
that many embedded devices use, rather than trying to query the device?
您可能很幸运,拥有使用此服务 ID 的嵌入式芯片之一:
You might just luck out and have one of the embedded chips that uses this service ID:
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
将我的 UUID
设置为此服务 ID 对我有用.
Setting my UUID
to this service ID worked for me.
这篇关于如何找到正确的 UUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!