Android 扫描附近的蓝牙设备并连接蓝牙音响的示例

作者:叶应是叶 时间:2022-05-14 05:36:48 

写了一个可以扫描附近蓝牙设备的小Demo,可以查看蓝牙设备的设备名和Mac地址

代码量不多,很容易看懂


/**
* 作者:叶应是叶
* 时间:2017/9/8 20:13
* 描述:
*/
public class ScanDeviceActivity extends AppCompatActivity {

private LoadingDialog loadingDialog;

private DeviceAdapter deviceAdapter;

private BluetoothAdapter bluetoothAdapter;

private Handler handler = new Handler();

private BroadcastReceiver discoveryReceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
  switch (intent.getAction()) {
   case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
    showLoadingDialog("正在搜索附近的蓝牙设备");
    break;
   case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
    Toast.makeText(ScanDeviceActivity.this, "搜索结束", Toast.LENGTH_SHORT).show();
    hideLoadingDialog();
    break;
   case BluetoothDevice.ACTION_FOUND:
    BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    deviceAdapter.addDevice(bluetoothDevice);
    deviceAdapter.notifyDataSetChanged();
    break;
  }
 }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_scan_device);
 BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
 bluetoothAdapter = bluetoothManager.getAdapter();
 if (bluetoothAdapter == null) {
  Toast.makeText(this, "当前设备不支持蓝牙", Toast.LENGTH_SHORT).show();
  finish();
 }
 initView();
 registerDiscoveryReceiver();
 startScan();
}

@Override
protected void onDestroy() {
 super.onDestroy();
 handler.removeCallbacksAndMessages(null);
 unregisterReceiver(discoveryReceiver);
 if (bluetoothAdapter.isDiscovering()) {
  bluetoothAdapter.cancelDiscovery();
 }
}

private void initView() {
 ListView lv_deviceList = (ListView) findViewById(R.id.lv_deviceList);
 deviceAdapter = new DeviceAdapter(this);
 lv_deviceList.setAdapter(deviceAdapter);
}

private void registerDiscoveryReceiver() {
 IntentFilter intentFilter = new IntentFilter();
 intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
 intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
 intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
 intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
 registerReceiver(discoveryReceiver, intentFilter);
}

private void startScan() {
 if (!bluetoothAdapter.isEnabled()) {
  if (bluetoothAdapter.enable()) {
   handler.postDelayed(new Runnable() {
    @Override
    public void run() {
     scanDevice();
    }
   }, 1500);
  } else {
   Toast.makeText(this, "请求蓝牙权限被拒绝,请授权", Toast.LENGTH_SHORT).show();
  }
 } else {
  scanDevice();
 }
}

private void scanDevice() {
 if (bluetoothAdapter.isDiscovering()) {
  bluetoothAdapter.cancelDiscovery();
 }
 bluetoothAdapter.startDiscovery();
}

private void showLoadingDialog(String message) {
 if (loadingDialog == null) {
  loadingDialog = new LoadingDialog(this);
 }
 loadingDialog.show(message, true, false);
}

private void hideLoadingDialog() {
 if (loadingDialog != null) {
  loadingDialog.dismiss();
 }
}

}

此外,还可以通过利用反射来调用系统API,从而与支持蓝牙A2DP协议的蓝牙音响连接上,不过因为我只有一部不算严格意义上的蓝牙音响来做测试,所以这个功能并不确定是否适用于大多数蓝牙设备


/**
* 作者:叶应是叶
* 时间:2017/9/8 20:02
* 描述:
*/
public class ConnectA2dpActivity extends AppCompatActivity {

private DeviceAdapter deviceAdapter;

private BluetoothAdapter bluetoothAdapter;

private Handler handler = new Handler();

private BluetoothA2dp bluetoothA2dp;

private LoadingDialog loadingDialog;

private final String TAG = "ConnectA2dpActivity";

private BroadcastReceiver a2dpReceiver = new BroadcastReceiver() {

@Override
 public void onReceive(Context context, Intent intent) {
  switch (intent.getAction()) {
   case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
    int connectState = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
    if (connectState == BluetoothA2dp.STATE_DISCONNECTED) {
     Toast.makeText(ConnectA2dpActivity.this, "已断开连接", Toast.LENGTH_SHORT).show();
    } else if (connectState == BluetoothA2dp.STATE_CONNECTED) {
     Toast.makeText(ConnectA2dpActivity.this, "已连接", Toast.LENGTH_SHORT).show();
    }
    break;
   case BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED:
    int playState = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING);
    if (playState == BluetoothA2dp.STATE_PLAYING) {
     Toast.makeText(ConnectA2dpActivity.this, "处于播放状态", Toast.LENGTH_SHORT).show();
    } else if (playState == BluetoothA2dp.STATE_NOT_PLAYING) {
     Toast.makeText(ConnectA2dpActivity.this, "未在播放", Toast.LENGTH_SHORT).show();
    }
    break;
  }
 }
};

private BroadcastReceiver discoveryReceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
  switch (intent.getAction()) {
   case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
    showLoadingDialog("正在搜索蓝牙设备,搜索时间大约一分钟");
    break;
   case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
    Toast.makeText(ConnectA2dpActivity.this, "搜索蓝牙设备结束", Toast.LENGTH_SHORT).show();
    hideLoadingDialog();
    break;
   case BluetoothDevice.ACTION_FOUND:
    BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    deviceAdapter.addDevice(bluetoothDevice);
    deviceAdapter.notifyDataSetChanged();
    break;
   case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
    int status = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
    if (status == BluetoothDevice.BOND_BONDED) {
     Toast.makeText(ConnectA2dpActivity.this, "已连接", Toast.LENGTH_SHORT).show();
    } else if (status == BluetoothDevice.BOND_NONE) {
     Toast.makeText(ConnectA2dpActivity.this, "未连接", Toast.LENGTH_SHORT).show();
    }
    hideLoadingDialog();
    break;
  }
 }
};

private BluetoothProfile.ServiceListener profileServiceListener = new BluetoothProfile.ServiceListener() {

@Override
 public void onServiceDisconnected(int profile) {
  if (profile == BluetoothProfile.A2DP) {
   Toast.makeText(ConnectA2dpActivity.this, "onServiceDisconnected", Toast.LENGTH_SHORT).show();
   bluetoothA2dp = null;
  }
 }

@Override
 public void onServiceConnected(int profile, final BluetoothProfile proxy) {
  if (profile == BluetoothProfile.A2DP) {
   Toast.makeText(ConnectA2dpActivity.this, "onServiceConnected", Toast.LENGTH_SHORT).show();
   bluetoothA2dp = (BluetoothA2dp) proxy;
  }
 }
};

private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  BluetoothDevice device = deviceAdapter.getDevice(position);
  if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
   Toast.makeText(ConnectA2dpActivity.this, "已连接该设备", Toast.LENGTH_SHORT).show();
   return;
  }
  showLoadingDialog("正在连接");
  connectA2dp(device);
 }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_connect_a2dp);
 BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
 bluetoothAdapter = bluetoothManager.getAdapter();
 if (bluetoothAdapter == null || !getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
  Toast.makeText(ConnectA2dpActivity.this, "当前设备不支持蓝牙", Toast.LENGTH_SHORT).show();
  finish();
 }
 bluetoothAdapter.getProfileProxy(this, profileServiceListener, BluetoothProfile.A2DP);
 initView();
 registerDiscoveryReceiver();
 registerA2dpReceiver();
 startScan();
}

@Override
protected void onDestroy() {
 super.onDestroy();
 handler.removeCallbacksAndMessages(null);
 unregisterReceiver(a2dpReceiver);
 unregisterReceiver(discoveryReceiver);
 if (bluetoothAdapter.isDiscovering()) {
  bluetoothAdapter.cancelDiscovery();
 }
}

private void initView() {
 ListView lv_deviceList = (ListView) findViewById(R.id.lv_deviceList);
 deviceAdapter = new DeviceAdapter(this);
 lv_deviceList.setAdapter(deviceAdapter);
 lv_deviceList.setOnItemClickListener(itemClickListener);
}

private void registerDiscoveryReceiver() {
 IntentFilter intentFilter = new IntentFilter();
 intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
 intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
 intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
 intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
 registerReceiver(discoveryReceiver, intentFilter);
}

private void registerA2dpReceiver() {
 IntentFilter intentFilter = new IntentFilter();
 intentFilter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
 intentFilter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);
 registerReceiver(a2dpReceiver, intentFilter);
}

private void startScan() {
 if (!bluetoothAdapter.isEnabled()) {
  if (bluetoothAdapter.enable()) {
   handler.postDelayed(new Runnable() {
    @Override
    public void run() {
     scanDevice();
    }
   }, 1500);
  } else {
   Toast.makeText(ConnectA2dpActivity.this, "请求蓝牙权限被拒绝", Toast.LENGTH_SHORT).show();
  }
 } else {
  scanDevice();
 }
}

private void scanDevice() {
 if (bluetoothAdapter.isDiscovering()) {
  bluetoothAdapter.cancelDiscovery();
 }
 bluetoothAdapter.startDiscovery();
}

public void setPriority(BluetoothDevice device, int priority) {
 try {
  Method connectMethod = BluetoothA2dp.class.getMethod("setPriority", BluetoothDevice.class, int.class);
  connectMethod.invoke(bluetoothA2dp, device, priority);
 } catch (Exception e) {
  e.printStackTrace();
 }
}

private void connectA2dp(BluetoothDevice bluetoothDevice) {
 if (bluetoothA2dp == null || bluetoothDevice == null) {
  return;
 }
 setPriority(bluetoothDevice, 100);
 try {
  Method connectMethod = BluetoothA2dp.class.getMethod("connect", BluetoothDevice.class);
  connectMethod.invoke(bluetoothA2dp, bluetoothDevice);
 } catch (Exception e) {
  e.printStackTrace();
 }
}

private void showLoadingDialog(String message) {
 if (loadingDialog == null) {
  loadingDialog = new LoadingDialog(this);
 }
 loadingDialog.show(message, true, false);
}

private void hideLoadingDialog() {
 if (loadingDialog != null) {
  loadingDialog.dismiss();
 }
}

}

这里给出源代码供大家参考:BluetoothDemo

来源:http://www.jianshu.com/p/e2a24e871575?utm_source=tuicool&utm_medium=referral

标签:android,扫描,蓝牙
0
投稿

猜你喜欢

  • C#中Hashtable和Dictionary的区别与用法示例

    2023-10-05 03:22:59
  • Android实现Activity、Service与Broadcaster三大组件之间互相调用的方法详解

    2023-04-09 08:51:30
  • C#使用二分查找法判断指定字符的方法

    2022-08-02 06:46:30
  • springboot日期转换器实现实例解析

    2023-01-31 13:16:45
  • C# WinForm创建Excel文件的实例

    2023-08-06 19:47:06
  • C#开发Windows UWP系列之布局面板RelativePanel

    2023-04-11 12:28:29
  • feign GET请求不支持对象传参的坑及解决

    2023-07-29 18:10:25
  • Java中高效判断数组中是否包含某个元素的几种方法

    2022-02-21 05:46:39
  • java中的session对象及其常用方法小结

    2023-11-25 02:31:33
  • JDK1.8下载、安装和环境配置超详细教程(最新最完整)

    2022-07-22 12:58:34
  • 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)

    2022-11-09 11:40:37
  • Java中的vector类使用示例小结

    2023-09-01 02:46:59
  • Java中if...else语句使用的学习教程

    2023-10-08 03:30:59
  • c#实现16进制和字符串之间转换的代码

    2023-07-22 08:28:09
  • JavaCV实现读取视频信息及自动截取封面图详解

    2022-05-20 14:43:05
  • JavaWeb 中Cookie实现记住密码的功能示例

    2023-04-06 05:06:48
  • Android listview的滑动冲突解决方法

    2022-07-19 02:50:59
  • C#实现数据包加密与解密实例详解

    2022-05-14 18:44:50
  • Java中BigDecimal类的add()的使用详解

    2023-03-07 16:12:11
  • SpringBoot+Netty+WebSocket实现消息发送的示例代码

    2023-08-16 00:02:52
  • asp之家 软件编程 m.aspxhome.com