Android蓝牙搜索服务可用设备

时间:2015-10-07 11:39:19

标签: java android background-process

我正在尝试在后台服务中发现可用的蓝牙设备。我被困在“startActivityForResult”部分,因为服务不是活动。请帮忙......

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);


      }


}

1 个答案:

答案 0 :(得分:0)

您必须在服务开始前激活蓝牙。

只需将此代码添加到您的活动中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == <needed value>) {


    }
    super.onActivityResult(requestCode, resultCode, data);
}