最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年09月09日 09:29:40   作者:葉應(yīng)是葉  
本篇文章主要介紹了Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例,具有一定的參考價值,有興趣的可以了解一下

寫了一個可以掃描附近藍(lán)牙設(shè)備的小Demo,可以查看藍(lán)牙設(shè)備的設(shè)備名和Mac地址

代碼量不多,很容易看懂

/**
 * 作者:葉應(yīng)是葉
 * 時間: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, "請求藍(lán)牙權(quán)限被拒絕,請授權(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)格意義上的藍(lán)牙音響來做測試,所以這個功能并不確定是否適用于大多數(shù)藍(lán)牙設(shè)備

/**
 * 作者:葉應(yīng)是葉
 * 時間: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è)備,搜索時間大約一分鐘");
     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, "請求藍(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android中將View的內(nèi)容保存為圖像的簡單實例

    Android中將View的內(nèi)容保存為圖像的簡單實例

    這篇文章主要介紹了Android中將View的內(nèi)容保存為圖像的簡單實例,有需要的朋友可以參考一下
    2014-01-01
  • Android中ScrollView嵌套GridView的解決辦法

    Android中ScrollView嵌套GridView的解決辦法

    前些日子在開發(fā)中用到了需要ScrollView嵌套GridView的情況,由于這兩款控件都自帶滾動條,當(dāng)他們碰到一起的時候便會出問題,即GridView會顯示不全。下面小編給大家分享下解決方案,需要的朋友可以參考下
    2017-04-04
  • Android中的Activity生命周期總結(jié)

    Android中的Activity生命周期總結(jié)

    這篇文章主要介紹了Android中的Activity生命周期總結(jié),本文講解了Activity四大基本狀態(tài)、Activity七大生命周期函數(shù)、切換橫豎屏觸發(fā)的生命周期事件等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • AndroidStudio接入Unity工程并實現(xiàn)相互跳轉(zhuǎn)的示例代碼

    AndroidStudio接入Unity工程并實現(xiàn)相互跳轉(zhuǎn)的示例代碼

    這篇文章主要介紹了AndroidStudio接入Unity工程并實現(xiàn)相互跳轉(zhuǎn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Flutter?Android多窗口方案落地實戰(zhàn)

    Flutter?Android多窗口方案落地實戰(zhàn)

    這篇文章主要為大家介紹了Flutter?Android多窗口方案落地實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Android中l(wèi)ayer-list基本使用詳解

    Android中l(wèi)ayer-list基本使用詳解

    這篇文章主要介紹了Android中l(wèi)ayer-list基本使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Android仿微信錄音功能(錄音后的raw文件轉(zhuǎn)mp3文件)

    Android仿微信錄音功能(錄音后的raw文件轉(zhuǎn)mp3文件)

    這篇文章主要介紹了Android中仿微信錄音功能錄音后的raw文件轉(zhuǎn)mp3文件,本文通過實例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下
    2019-11-11
  • Android實現(xiàn)底部圖片選擇Dialog

    Android實現(xiàn)底部圖片選擇Dialog

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)底部圖片選擇Dialog,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • android AlertDialog的簡單使用實例

    android AlertDialog的簡單使用實例

    本篇文章主要介紹了android AlertDialog的簡單使用實例,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • android 復(fù)制 粘貼 剪切功能應(yīng)用

    android 復(fù)制 粘貼 剪切功能應(yīng)用

    網(wǎng)上有很多android 復(fù)制 粘貼 剪切功能的文章,只是放到自己的程序中不知道如何處理,現(xiàn)在尋得一可行方法,需要的朋友可以參考下
    2012-11-11

最新評論

东阳市| 张家口市| 扶沟县| 兴安盟| 贡山| 秦皇岛市| 永宁县| 娱乐| 临朐县| 卢湾区| 荥经县| 长沙县| 祁连县| 井冈山市| 侯马市| 桃江县| 阿鲁科尔沁旗| 楚雄市| 尼勒克县| 南汇区| 中超| 安阳县| 同心县| 洪泽县| 汉阴县| 鹿邑县| 乌拉特后旗| 米易县| 遵义县| 河津市| 清水县| 武威市| 浦县| 绥江县| 迭部县| 贡嘎县| 盖州市| 乌鲁木齐县| 淳安县| 涟水县| 鹤岗市|