我是新來的android和我正在與藍牙功能的應用程序。我能夠設置藍牙適配器,獲取我自己的設備信息,但我無法使用startdiscovery發現藍牙設備。當我開始掃描時,它什麼都不做。Android上的藍牙:StartDiscovery無法正常工作。無法掃描設備
我使用的是onclicklistner開始掃描:
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!(bluetooth.isEnabled())) {
status = "Bluetooth is not Enabled.";
Toast.makeText(AddUser.this, status, Toast.LENGTH_LONG).show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
else
{
scand();
}
}
這是我剛纔的「公共無效的onCreate」功能後,把onActivityResult功能:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
System.out.println(resultCode);
if (resultCode == RESULT_CANCELED) {
status="Error Enabling bluetooth";
Toast.makeText(AddUser.this, status, Toast.LENGTH_LONG).show();
} else {
scand();
}
}
這是我的scand功能,其中我是callng startdiscovery:
private void scand()
{
bluetooth.startDiscovery();
Log.d("itmes", ""+items.size());
item1 = new String[items.size()];
item1 = items.toArray(item1);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose a device");
builder.setItems(item1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), item1[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
這是broadca stReceiver:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
Log.e("br", "--- device found ---");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
items.add(device.getName());
}
}
};
在爲廣播接收器上面的代碼中,我試圖把發現的設備名稱字符串中的ArrayList的「項目」。
我的OnCreate中functon內註冊的BroadcastReceiver這樣的:
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
我已經設置在androidmanifest文件藍牙權限。在上面的scand函數中,假設顯示已發現設備的列表,但它只顯示一個空標題的對話框。請告訴我如何正確使用startdiscovery和broadcastreceiver在alertdialog中顯示結果。
是你試試藍牙設備在發現模式下掃描? – 2013-04-24 06:00:38
是的,它們是可以發現的。 – 2013-04-24 06:04:45
是使用startdiscovery和broadcastreceiver的正確方法嗎? – 2013-04-24 06:08:04