[英]Android Bluetooth search in service for available devices
I am trying to discover available blueetooth devices in background service. 我试图在后台服务中发现可用的蓝牙设备。 I am stuck at the part "startActivityForResult" as service is not an Activity.
我被困在“ startActivityForResult”部分,因为服务不是活动。 please help...
请帮忙...
private void CheckBTState() {
// TODO Auto-generated method stub
// Check for Bluetooth support and then check to make sure it is turned on
// If it isn't request to turn it on
// List paired devices
// Emulator doesn't support Bluetooth and will return null
Log.d("check","4");
if (btAdapter.isEnabled()) {
// out.append("\nBluetooth is enabled...");
// Starting the device discovery
btAdapter.startDiscovery();
Log.d("check","5");
} else {
Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
Log.d("check","6");
STUCK OVER HERE.... DON'T NO HOW TO CALL startActivityForResult IN SERVICE
// ((Activity) context).startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
// activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
You have to activate BlueTooth before service started. 服务启动之前,您必须先激活蓝牙。
Just add to your Activity this code: 只需将以下代码添加到您的活动中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == <needed value>) {
}
super.onActivityResult(requestCode, resultCode, data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.