Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例
寫(xiě)了一個(gè)可以掃描附近藍(lán)牙設(shè)備的小Demo,可以查看藍(lán)牙設(shè)備的設(shè)備名和Mac地址
代碼量不多,很容易看懂
/**
* 作者:葉應(yīng)是葉
* 時(shí)間: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("正在搜索附近的藍(lán)牙設(shè)備");
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
Toast.makeText(ScanDeviceActivity.this, "搜索結(jié)束", 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, "當(dāng)前設(shè)備不支持藍(lán)牙", 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, "請(qǐng)求藍(lán)牙權(quán)限被拒絕,請(qǐng)授權(quán)", 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();
}
}
}
此外,還可以通過(guò)利用反射來(lái)調(diào)用系統(tǒng)API,從而與支持藍(lán)牙A2DP協(xié)議的藍(lán)牙音響連接上,不過(guò)因?yàn)槲抑挥幸徊坎凰銍?yán)格意義上的藍(lán)牙音響來(lái)做測(cè)試,所以這個(gè)功能并不確定是否適用于大多數(shù)藍(lán)牙設(shè)備
/**
* 作者:葉應(yīng)是葉
* 時(shí)間: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, "已斷開(kāi)連接", 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, "處于播放狀態(tài)", 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("正在搜索藍(lán)牙設(shè)備,搜索時(shí)間大約一分鐘");
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
Toast.makeText(ConnectA2dpActivity.this, "搜索藍(lán)牙設(shè)備結(jié)束", 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, "已連接該設(shè)備", 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, "當(dāng)前設(shè)備不支持藍(lán)牙", 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, "請(qǐng)求藍(lán)牙權(quán)限被拒絕", 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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中將View的內(nèi)容保存為圖像的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android中將View的內(nèi)容保存為圖像的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2014-01-01
Android中ScrollView嵌套GridView的解決辦法
前些日子在開(kāi)發(fā)中用到了需要ScrollView嵌套GridView的情況,由于這兩款控件都自帶滾動(dòng)條,當(dāng)他們碰到一起的時(shí)候便會(huì)出問(wèn)題,即GridView會(huì)顯示不全。下面小編給大家分享下解決方案,需要的朋友可以參考下2017-04-04
AndroidStudio接入U(xiǎn)nity工程并實(shí)現(xiàn)相互跳轉(zhuǎn)的示例代碼
這篇文章主要介紹了AndroidStudio接入U(xiǎn)nity工程并實(shí)現(xiàn)相互跳轉(zhuǎn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Flutter?Android多窗口方案落地實(shí)戰(zhàn)
這篇文章主要為大家介紹了Flutter?Android多窗口方案落地實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android仿微信錄音功能(錄音后的raw文件轉(zhuǎn)mp3文件)
這篇文章主要介紹了Android中仿微信錄音功能錄音后的raw文件轉(zhuǎn)mp3文件,本文通過(guò)實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2019-11-11
Android實(shí)現(xiàn)底部圖片選擇Dialog
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部圖片選擇Dialog,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
android AlertDialog的簡(jiǎn)單使用實(shí)例
本篇文章主要介紹了android AlertDialog的簡(jiǎn)單使用實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
android 復(fù)制 粘貼 剪切功能應(yīng)用
網(wǎng)上有很多android 復(fù)制 粘貼 剪切功能的文章,只是放到自己的程序中不知道如何處理,現(xiàn)在尋得一可行方法,需要的朋友可以參考下2012-11-11

