我正在学习在Android中使用蓝牙。我已在清单文件
中授予此权限<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
主要代码在这里:
private static final int REQUEST_ENABLE_BT = 0;
private static final int REQUEST_DISCOVERABLE_BT = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView out=(TextView)findViewById(R.id.out);
final Button button = (Button) findViewById(R.id.button1);
final Button button1 = (Button) findViewById(R.id.button2);
final Button button2 = (Button) findViewById(R.id.button3);
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
out.append("device not supported");
}
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (!mBluetoothAdapter.isDiscovering()) {
Context context = getApplicationContext();
CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mBluetoothAdapter.disable();
Context context = getApplicationContext();
CharSequence text = "TURNING_OFF BLUETOOTH";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});
}
但它告诉我:
屏幕上的设备不受支持
并点击一个按钮会让app强制关闭,这样我的android模拟器会出现什么问题?
答案 0 :(得分:3)
在您的应用程序中使用蓝牙之前,您需要阅读this有关Emulator的一些限制。
答案 1 :(得分:1)
模拟器不支持蓝牙,使用真实设备
答案 2 :(得分:1)
device not supported
是您在
if (mBluetoothAdapter == null) {
out.append("device not supported");
}
这是因为Android模拟器不支持Bluetooh。您需要物理设备来检查蓝牙应用程序。
检查这些问题以获取更多信息
How to use Bluetooth in Android emulator?
Bluetooth support on Android Emulator
答案 3 :(得分:0)
如果不支持蓝牙,那么为什么模拟器有一个qemu -bt选项?为什么有蓝牙的usb键盘仿真?需要启用蓝牙,因此它显示为支持。除了向BoardConfig.mk添加BOARD_HAVE_BLUETOOTH:= true之外,可能还需要特定的配置或特定目标?
如何配置蓝牙以便在模拟器中启用?
BT [:HCI型] 蓝牙适配器的类型指定格式与'-bt hci'选项相同,请参阅允许的HCI类型。如果没有给出类型,则HCI逻辑对应于-bt hci,vlan = 0。该USB设备实现了HCI的USB传输层。用法示例:
qemu [... OPTIONS ...] -usbdevice bt:hci,vlan = 3 -bt device:keyboard,vlan = 3