我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