国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例

2019-10-22 18:28:12
字體:
供稿:網(wǎng)友

寫了一個(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();  } }}

此外,還可以通過利用反射來調(diào)用系統(tǒng)API,從而與支持藍(lán)牙A2DP協(xié)議的藍(lán)牙音響連接上,不過因?yàn)槲抑挥幸徊坎凰銍?yán)格意義上的藍(lán)牙音響來做測試,所以這個(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, "已斷開連接", 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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泸州市| 郁南县| 揭东县| 江川县| 油尖旺区| 图片| 寻甸| 屏边| 沙田区| 措美县| 囊谦县| 土默特右旗| 翁源县| 黔西县| 永城市| 阿图什市| 方山县| 嘉义市| 光泽县| 洱源县| 永福县| 新乐市| 弥勒县| 达拉特旗| 衡南县| 兴义市| 天等县| 长寿区| 禄劝| 三原县| 安达市| 江山市| 固阳县| 西充县| 海丰县| 鄂尔多斯市| 宣化县| 新巴尔虎左旗| 紫阳县| 东海县| 赤壁市|