5
這裏是我使用的測試代碼:如何使用Android藍牙設備選取器檢索藍牙設備信息?
public class IOConnectDirect extends Activity {
private static final String TAG = "IOConnectDirect";
private static final int REQCODE_BLUETOOTH_RESULT = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
setTitle(getTitle() + "--" + TAG);
Intent intentBluetooth = new Intent();
intentBluetooth.setAction("android.bluetooth.devicepicker.action.LAUNCH");
//android.bluetooth.devicepicker.action.DEVICE_SELECTED not working .
startActivityForResult(intentBluetooth, REQCODE_BLUETOOTH_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "onActivityResult(" + requestCode +"," + resultCode + ")");
switch (requestCode) {
case REQCODE_BLUETOOTH_RESULT:
Log.i(TAG, "requestCode = REQCODE_BLUETOOTH_RESULT");
if(resultCode == RESULT_OK) {
Log.i(TAG, "RESULT_OK");
// Retrieve the Info
Bundle extras = data.getExtras();
if(extras != null) {
Log.i(TAG, "Bundle ok");
}
}
else {
Log.i(TAG, "!RESULT_OK = FAILED(" + resultCode + ")");
Toast.makeText(this, "Failed(" + resultCode +")", Toast.LENGTH_SHORT).show();
}
break;
default:
Log.i(TAG, "requestCode = ????");
break;
}
}
}
這裏是logcat的輸出:
I/IOConnectDirect(14956): onActivityResult(0,0)
I/IOConnectDirect(14956): requestCode = REQCODE_BLUETOOTH_RESULT
I/IOConnectDirect(14956): !RESULT_OK = FAILED(0)
代碼工作(你需要首先激活藍牙),我我只是無法讓它做我想做的事,即檢索我從活動中選擇的藍牙設備名稱和地址。
注:
- 我沒有嘗試連接到我只是想選擇的設備信息。
- 我熟悉的其他方法來做到這一點像在Android藍牙聊天樣品
UPDATE
我最終使用一個BroadcastReceiver
public class IOConnectDirect extends Activity {
private static final String TAG = "IOConnectDirect";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
BluetoothConnectActivityReceiver mBluetoothPickerReceiver = new BluetoothConnectActivityReceiver();
registerReceiver(mBluetoothPickerReceiver, new IntentFilter(BluetoothDevicePicker.ACTION_DEVICE_SELECTED));
startActivity(new Intent(BluetoothDevicePicker.ACTION_LAUNCH)
.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false)
.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE, BluetoothDevicePicker.FILTER_TYPE_ALL)
.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS));
}
public class BluetoothConnectActivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(BluetoothDevicePicker.ACTION_DEVICE_SELECTED.equals(intent.getAction())) {
context.unregisterReceiver(this);
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(context, "device" + device.getAddress(), Toast.LENGTH_SHORT).show();
}
}
}
我已經有工作的選擇,但我想使用Android提供了一個。 – Rick 2012-01-03 15:10:47