我m建立一个android蓝牙聊天应用程序,并面临一些问题。我的问题是:我无法检测范围内的可用蓝牙设备,无法显示在列表中。作为即时新到android编程无法检测到问题。请帮助我。android蓝牙聊天应用程序
我的代码是:
public class BluetoothSearchActivity extends Activity {
ArrayAdapter<String> btArrayAdapter;
BluetoothAdapter mBluetoothAdapter;
TextView stateBluetooth;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView BluetoothSearchImageView=new ImageView(this);
BluetoothSearchImageView.setImageResource(R.drawable.inner1);
setContentView(BluetoothSearchImageView);
setContentView(R.layout.activity_bluetooth_search);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
ListView listDevicesFound=(ListView) findViewById(R.id.myList);
btArrayAdapter=new ArrayAdapter<String> (BluetoothSearchActivity.this,android.R.layout.simple_list_item_1);
listDevicesFound.setAdapter(btArrayAdapter);
registerReceiver(ActionFoundReceiver,new IntentFilter(BluetoothDevice.ACTION_FOUND));
btArrayAdapter.clear();
mBluetoothAdapter.startDiscovery();
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(ActionFoundReceiver);
}
private final BroadcastReceiver ActionFoundReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
btArrayAdapter.add(device.getName()+"\n"+device.getAddress());
btArrayAdapter.notifyDataSetChanged();
}
}
};
你拥有所有正确的权限? – thepoosh