我的应用程序需要发现附近的蓝牙设备。我的工作流程如下所示:
[检查蓝牙状态->启用IF->禁用它,然后启用并搜索附近的设备,否则,请启用它并搜索附近的设备。
我正在使用广播接收器来获取附近的设备,但是问题是接收器没有被触发。
下面是我的代码。
MyFragment.java:
public class MyFragment extends Fragment {
private BluetoothAdapter mBluetoothAdapter;
private ProgressDialog dialog;
private AlertDialog.Builder alertDialog;
boolean isBluetoothEnabledBySystem = false;
View view;
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_my_fragment, container, false);
checkForBluetoothState();
return view;
}
private void checkForBluetoothState()
{
dialog = new ProgressDialog(getContext());
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setCancelable(false);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null){
if (mBluetoothAdapter.isEnabled())
{
if (isBluetoothEnabledBySystem == false)
{
askUserIfAnyDeviceConnected();
}
}
else
{
new EnableBluetooth().execute();
}
}else
{
Toast.makeText(getActivity(), "Bluetooth not supported", Toast.LENGTH_SHORT).show();
}
}
private void askUserIfAnyDeviceConnected() {
alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setCancelable(false);
alertDialog.setTitle("Oops!");
alertDialog.setMessage("It seems like your device is connected with another device via bluetooth.\nWould you like to disconnect it?");
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new DisableBluetooth().execute();
}
});
alertDialog.setNegativeButton("Later", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("later","later");
}
}).create().show();
}
private class EnableBluetooth extends AsyncTask<Void,Void,Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Enabling Bluetooth");
dialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(2000);
mBluetoothAdapter.enable();
isBluetoothEnabledBySystem = true;
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
DiscoverBluetoothDevices();
}
}
private class DisableBluetooth extends AsyncTask<Void,Void,Void>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.setMessage("Disabling Bluetooth");
dialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(2000);
mBluetoothAdapter.disable();
mBluetoothAdapter.enable();
isBluetoothEnabledBySystem = true;
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (dialog != null && dialog.isShowing())
{
dialog.setMessage("Device enabled");
dialog.show();
DiscoverBluetoothDevices();
}
}
}
private void DiscoverBluetoothDevices()
{
dialog.setMessage("Discovering Device");
dialog.show();
Log.d("adapter status","adapter status"+mBluetoothAdapter);
mBluetoothAdapter.startDiscovery();
IntentFilter bluetoothIntentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
getContext().registerReceiver(bluetoothBroadcast,bluetoothIntentFilter);
}
private final BroadcastReceiver bluetoothBroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("called receiver","called receiver");
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// A Bluetooth device was found
// Getting device information from the intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i("Device found", "Device found: " + device.getName() + "; MAC " + device.getAddress());
}
}
};
@Override
public void onPause() {
super.onPause();
getContext().unregisterReceiver(bluetoothBroadcast);
}
@Override
public void onDestroy() {
super.onDestroy();
getContext().unregisterReceiver(bluetoothBroadcast);
}
}
让我知道错误/错误在哪里。预先感谢。
我认为这是您正在寻找的那个:
Android Broadcast Receiver bluetooth events catching
还检查清单文件或添加所需的文件:
<uses-feature android:name="android.hardware.bluetooth" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
还要在清单中添加广播接收器,确保您添加了类似于此的意图过滤器
<receiver android:name=".MyBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter>