寫了一個可以掃描附近藍牙設備的小Demo,可以查看藍牙設備的設備名和Mac地址

成都創(chuàng)新互聯(lián)公司專注于東遼企業(yè)網站建設,響應式網站設計,成都商城網站開發(fā)。東遼網站建設公司,為東遼等地區(qū)提供建站服務。全流程按需設計,專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務
代碼量不多,很容易看懂
/**
* 作者:葉應是葉
* 時間: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();
}
}
}
此外,還可以通過利用反射來調用系統(tǒng)API,從而與支持藍牙A2DP協(xié)議的藍牙音響連接上,不過因為我只有一部不算嚴格意義上的藍牙音響來做測試,所以這個功能并不確定是否適用于大多數(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, "已斷開連接", 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("正在搜索藍牙設備,搜索時間大約一分鐘");
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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網站欄目:Android掃描附近的藍牙設備并連接藍牙音響的示例
標題路徑:http://www.chinadenli.net/article42/ishdhc.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供品牌網站設計、外貿建站、小程序開發(fā)、App設計、企業(yè)建站、網站策劃
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)