gpt4 book ai didi

android - 在 Android 2.0 中查找 UUID

转载 作者:行者123 更新时间:2023-11-29 16:13:51 26 4
gpt4 key购买 nike

我正在编写一个需要在 Android 2.0 中运行的程序。我目前正在尝试将我的 android 设备连接到嵌入式蓝牙芯片。我已获得有关使用 fetchuidsWithSDP() 或 getUuids() 的信息,但我阅读的页面解释说这些方法隐藏在 2.0 SDK 中,必须使用反射调用。我不知道那是什么意思,也没有任何解释。给出了示例代码,但背后的解释很少。我希望有人能帮助我理解这里到底发生了什么,因为我是 Android 开发的新手。

String action = "android.bleutooth.device.action.UUID";
IntentFilter filter = new IntentFilter( action );
registerReceiver( mReceiver, filter );

我阅读的页面还说,在第一行中,蓝牙故意拼写为“bleutooth”。如果有人能解释这一点,我将不胜感激,但对我来说这毫无意义,除非开发人员打错了字。

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive( Context context, Intent intent ) {
BluetoothDevice deviceExtra = intent.getParcelableExtra("android.bluetooth.device.extra.Device");
Parcelable[] uuidExtra = intent.getParcelableArrayExtra("android.bluetooth.device.extra.UUID");
}

};

我无法理解如何为我的嵌入式蓝牙芯片找到正确的 UUID。如果有人可以提供帮助,我们将不胜感激。

编辑:我将添加我的 onCreate() 方法的其余部分,以便您可以看到我正在使用的内容。

 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Set up window View
setContentView(R.layout.main);

// Initialize the button to scan for other devices.
btnScanDevice = (Button) findViewById( R.id.scandevice );

// Initialize the TextView which displays the current state of the bluetooth
stateBluetooth = (TextView) findViewById( R.id.bluetoothstate );
startBluetooth();

// Initialize the ListView of the nearby bluetooth devices which are found.
listDevicesFound = (ListView) findViewById( R.id.devicesfound );
btArrayAdapter = new ArrayAdapter<String>( AndroidBluetooth.this,
android.R.layout.simple_list_item_1 );
listDevicesFound.setAdapter( btArrayAdapter );

CheckBlueToothState();

// Add an OnClickListener to the scan button.
btnScanDevice.setOnClickListener( btnScanDeviceOnClickListener );

// Register an ActionFound Receiver to the bluetooth device for ACTION_FOUND
registerReceiver( ActionFoundReceiver, new IntentFilter( BluetoothDevice.ACTION_FOUND ) );

// Add an item click listener to the ListView
listDevicesFound.setOnItemClickListener( new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
// Save the device the user chose.
myBtDevice = btDevicesFound.get( arg2 );

// Open a socket to connect to the device chosen.
try {
btSocket = myBtDevice.createRfcommSocketToServiceRecord( MY_UUID );
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "Bluetooth not available, or insufficient permissions" );
} catch ( NullPointerException e ) {
Log.e( "Bluetooth Socket", "Null Pointer One" );
}

// Cancel the discovery process to save battery.
myBtAdapter.cancelDiscovery();

// Update the current state of the Bluetooth.
CheckBlueToothState();

// Attempt to connect the socket to the bluetooth device.
try {
btSocket.connect();
// Open I/O streams so the device can send/receive data.
iStream = btSocket.getInputStream();
oStream = btSocket.getOutputStream();
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "IO Exception" );
} catch ( NullPointerException e ) {
Log.e( "Bluetooth Socket", "Null Pointer Two" );
}
}
});
}

最佳答案

您最好使用同步版本,这样您就不必处理设置 BroadcastReceiver 的所有移动部分。由于您总是在发现之后执行此操作,因此缓存的数据将始终是最新的。

这里的功能是将 UUID 数据封装到一个方法中。此代码位于您链接的博客文章的评论之一中:

//In SDK15 (4.0.3) this method is now public as
//Bluetooth.fetchUuisWithSdp() and BluetoothDevice.getUuids()
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;
}
}

然后您可以在代码中的任何位置调用此方法,向其传递一个 BluetoothDevice 并返回该设备服务的 UUID 数组(通常对于小型嵌入式堆栈,该数组只有一项);像这样的东西:

  // Save the device the user chose.
myBtDevice = btDevicesFound.get( arg2 );
//Query the device's services
ParcelUuid[] uuids = servicesFromDevice(myBtDevice);

// Open a socket to connect to the device chosen.
try {
btSocket = myBtDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
} catch ( IOException e ) {
Log.e( "Bluetooth Socket", "Bluetooth not available, or insufficient permissions" );
} catch ( NullPointerException e ) {
Log.e( "Bluetooth Socket", "Null Pointer One" );
}

在您上面发布的区 block 中。

作为旁注,以您拥有的方式调用所有这些代码会让您的应用程序以后感到难过。调用 connect() 和获取流的代码块应该在后台线程上完成,因为该方法会阻塞一段时间,在主线程上调用此代码会暂时卡住您的 UI。您应该将该代码移动到 AsyncTaskThread 中,就像 SDK 中的 BluetoothChat 示例一样。

HTH

关于android - 在 Android 2.0 中查找 UUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11003280/

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