更新时间:2023-01-24 08:19:27
对于 1) 如果您还没有这样做,请添加
For 1) Well if you haven't done so , add
> import java.util.Set;
在您的导入语句中.这将解决设置"错误.
in your import statements . This will resolve "Set" error.
对于2)声明并初始化
mArrayAdapter
例如在你的活动中做:
private ArrayAdapter<String> mArrayAdapter;
然后 onCreate:
mArrayAdapter= new ArrayAdapter<String>(this, <your layout file>);
然后将其添加到 ListView
which should then be added to a ListView
//为新发现的设备查找并设置 ListView
// Find and set up the ListView for newly discovered devices
ListView newDevicesListView = (ListView)
findViewById(R.id.<layout_file>);
newDevicesListView.setAdapter(mArrayAdapter);
newDevicesListView.setOnItemClickListener(mDeviceClickListener);
参考 Android 示例中的蓝牙聊天示例.它应该可以帮助您使用蓝牙 api 的
评论更新:
如果您仔细查看 BT 示例中的 BluetoothChat.java 文件,您会看到这个
If you look closely on BluetoothChat.java file in BT example, you'll see this
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(D) Log.d(TAG, "onActivityResult " + resultCode);
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
// Get the BLuetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
// Attempt to connect to the device
mChatService.connect(device);
}
break;
case REQUEST_ENABLE_BT:
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK) {
// Bluetooth is now enabled, so set up a chat session
setupChat();
} else {
// User did not enable Bluetooth or an error occured
Log.d(TAG, "BT not enabled");
Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
finish();
}
}
}
观看这一行:
// Attempt to connect to the device
mChatService.connect(device);
此功能连接蓝牙设备.第一次它会要求你自动配对.配对后,下次它会自动连接到蓝牙设备.
This function connects to bluetooth device. First time it'll ask you to pair it automatically. Once paired, next time it'll auto connect to the bluetooth device.