1 )如果尚未这样做,请添加
import java.util.Set;
在你的import语句中.这将解决“设置”错误。
2 )声明并初始化
阵列适配器
例如,在Activity中:
私有arrayadaptermarrayadapter;
然后在onCreate上:
marray adapter=new array adapter (this,
必须将其添加到ListView中
//findandsetupthelistviewfornewlydiscovereddevices
listviewnewdeviceslistview=(listview )
findviewbyid(r.id.);
newdeviceslistview.set adapter (marray adapter;
newdeviceslistview.setonitemclicklistener (mdeviceclicklistener );
请参阅Android示例中的蓝牙聊天示例。 应该有助于使用蓝牙API
更新评论:
如果仔细查看BT示例的BluetoothChat.java文件,就会发现这一点
publicvoidonactivityresult (intrequestcode,int resultCode,Intent data ) {
if(d ) log.d ) tag,' onActivityResult ' resultCode;
sitch (请求代码)
case request _ connect _ device :
//whendevicelistactivityreturnswithadevicetoconnect
结果代码==activity.result _ ok (if ) {
//Get the device MAC address
String address=data.getExtras (
. getstring (devicelistactivity.extra _ device _ address;
//get the bluetooth设备对象
蓝牙devicedevice=mbluetoothadapter.getremote device (address;
//Attempt to connect to the device
mchatservice.connect(device;
}
黑;
case REQUEST_ENABLE_BT:
//whentherequesttoenablebluetoothreturns
结果代码==activity.result _ ok (if ) {
//Bluetooth is now enabled,so set up a chat session
setupChat (;
} else {
//userdidnotenablebluetoothoranerroroccured
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;
此功能已连接到蓝牙设备。 第一次被要求自动配对。 配对后,下次将自动连接到蓝牙设备。