Android 藍芽音響開發
完整demo地址:github
1.開啟藍芽:
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
/**如果本地藍芽沒有開啟,則開啟*/
if (!mBluetoothAdapter.isEnabled()) {
// 我們通過startActivityForResult()方法發起的Intent將會在onActivityResult()回撥方法中獲取使用者的選擇,比如使用者單擊了Yes開啟,
// 那麼將會收到RESULT_OK的結果,
// 如果RESULT_CANCELED則代表使用者不願意開啟藍芽
Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(mIntent, ENABLE_BLUE);
} else {
Toast.makeText(this, "藍芽已開啟", Toast.LENGTH_SHORT).show();
}
監聽開啟的結果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ENABLE_BLUE) {
if (resultCode == RESULT_OK) {
Toast.makeText(this, "藍芽開啟成功", Toast.LENGTH_SHORT).show();
getBondedDevices();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "藍芽開始失敗", Toast.LENGTH_SHORT).show();
}
} else {
}
}
2.關閉藍芽:
/**關閉藍芽*/
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
}
3.設計藍芽為可見:
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 180);//180可見時間
startActivity(intent);
4.收索藍芽:
註冊廣播監聽搜尋的結果:
/**註冊搜尋藍芽receiver*/
mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
mFilter.addAction(BluetoothDevice.ACTION_FOUND);
mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver, mFilter);
開始的搜尋:
// 如果正在搜尋,就先取消搜尋
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
// 開始搜尋藍芽裝置,搜尋到的藍芽裝置通過廣播返回
mBluetoothAdapter.startDiscovery();
監聽搜尋的結果:
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
/** 搜尋到的藍芽裝置*/
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 搜尋到的不是已經配對的藍芽裝置
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
BlueDevice blueDevice = new BlueDevice();
blueDevice.setName(device.getName());
blueDevice.setAddress(device.getAddress());
blueDevice.setDevice(device);
setDevices.add(blueDevice);
blueAdapter.setSetDevices(setDevices);
blueAdapter.notifyDataSetChanged();
Log.d(MAINACTIVITY, "搜尋結果......"+device.getName());
}
/**當繫結的狀態改變時*/
} else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
/**搜尋完成*/
} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
setProgressBarIndeterminateVisibility(false);
Log.d(MAINACTIVITY, "搜尋完成......");
hideProgressDailog();
}
}
};
5.配對藍芽:
配對工具類
public class BlueUtils {
public BlueUtils(BlueDevice blueDevice) {
this.blueDevice = blueDevice;
}
/**
* 配對
*/
public void doPair() {
if(null == mOthHandler){
HandlerThread handlerThread = new HandlerThread("other_thread");
handlerThread.start();
mOthHandler = new Handler(handlerThread.getLooper());
}
mOthHandler.post(new Runnable() {
@Override
public void run() {
initSocket(); //取得socket
try {
socket.connect(); //請求配對
// mAdapterManager.updateDeviceAdapter();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
/**
* 取消藍芽配對
* @param device
*/
public static void unpairDevice(BluetoothDevice device) {
try {
Method m = device.getClass()
.getMethod("removeBond", (Class[]) null);
m.invoke(device, (Object[]) null);
} catch (Exception e) {
Log.d("BlueUtils", e.getMessage());
}
}
/**
* 取得BluetoothSocket
*/
private void initSocket() {
BluetoothSocket temp = null;
try {
Method m = blueDevice.getDevice().getClass().getMethod("createRfcommSocket", new Class[] {int.class});
temp = (BluetoothSocket) m.invoke(blueDevice.getDevice(), );
//怪異錯誤: 直接賦值給socket,對socket操作可能出現異常, 要通過中間變數temp賦值給socket
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
socket = temp;
}
}
註冊監聽配對結果的廣播(使用同上面的註冊程式碼)
/**註冊搜尋藍芽receiver*/
mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
mFilter.addAction(BluetoothDevice.ACTION_FOUND);
mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiver, mFilter);
開始配對
/**
* 開始配對藍芽裝置
*
* @param blueDevice
*/
private void startPariBlue(BlueDevice blueDevice) {
BlueUtils blueUtils = new BlueUtils(blueDevice);
blueUtils.doPair();
}
監聽配對結果:(使用同上面的廣播接收者)
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
/** 搜尋到的藍芽裝置*/
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
.....
/**當繫結的狀態改變時*/
} else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
switch (device.getBondState()) {
case BluetoothDevice.BOND_BONDING:
Log.d(MAINACTIVITY, "正在配對......");
break;
case BluetoothDevice.BOND_BONDED:
Log.d(MAINACTIVITY, "完成配對");
hideProgressDailog();
/**開始連線*/
contectBuleDevices();
break;
case BluetoothDevice.BOND_NONE:
Log.d(MAINACTIVITY, "取消配對");
Toast.makeText(MainActivity.this,"成功取消配對",Toast.LENGTH_SHORT).show();
getBondedDevices();
break;
default:
break;
}
/**搜尋完成*/
} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
....
}
}
};
6.使用A2DP協議連線藍芽裝置:
連線裝置
/**
* 開始連線藍芽裝置
*/
private void contectBuleDevices() {
/**使用A2DP協議連線裝置*/
mBluetoothAdapter.getProfileProxy(this, mProfileServiceListener, BluetoothProfile.A2DP);
}
監聽連線的回撥
/**
* 連線藍芽裝置(通過監聽藍芽協議的服務,在連線服務的時候使用BluetoothA2dp協議)
*/
private BluetoothProfile.ServiceListener mProfileServiceListener = new BluetoothProfile.ServiceListener() {
@Override
public void onServiceDisconnected(int profile) {
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
try {
if (profile == BluetoothProfile.HEADSET) {
....
} else if (profile == BluetoothProfile.A2DP) {
/**使用A2DP的協議連線藍芽裝置(使用了反射技術呼叫連線的方法)*/
a2dp = (BluetoothA2dp) proxy;
if (a2dp.getConnectionState(currentBluetoothDevice) != BluetoothProfile.STATE_CONNECTED) {
a2dp.getClass()
.getMethod("connect", BluetoothDevice.class)
.invoke(a2dp, currentBluetoothDevice);
Toast.makeText(MainActivity.this,"請播放音樂",Toast.LENGTH_SHORT).show();
getBondedDevices();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
7.新增許可權
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
8.開啟樂庫播放音樂
9.Android 6.0的系統需要動態新增許可權才能搜尋出藍芽裝置
Android 6.0的系統需要動態新增許可權
/**判斷手機系統的版本*/
if (Build.VERSION.SDK_INT >= 6.0) {//Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
/**動態新增許可權:ACCESS_FINE_LOCATION*/
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_REQUEST_CONSTANT);
}
}
請求許可權的回撥
/**請求許可權的回撥:這裡判斷許可權是否新增成功*/
/**請求許可權的回撥:這裡判斷許可權是否新增成功*/
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_REQUEST_CONSTANT: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("main","新增許可權成功");
}
return;
}
}
}
相關文章
- 電腦怎麼連藍芽音響 藍芽音響連線電腦步驟藍芽
- 音響怎麼連線手機藍芽 音響連線手機藍芽的方法藍芽
- Android開發--藍芽操作Android藍芽
- 電腦怎麼連線藍芽音響 臺式電腦連藍芽音響圖解藍芽圖解
- Android 傳統藍芽開發Android藍芽
- 藍芽小鋼炮 - Bose 博士 Revolve 藍芽音響使用感受藍芽
- MacBook如何連線多個藍芽音響?Mac藍芽
- 兩臺藍芽音響組立體聲藍芽
- Android藍芽開發流程實踐Android藍芽
- Android藍芽開發全面總結Android藍芽
- win10藍芽連線音響的方法_win10系統怎麼連線小米藍芽音響Win10藍芽
- 膝上型電腦怎麼連線藍芽音響裝置 筆記本和藍芽音響連線藍芽筆記
- win10藍芽音響沒有聲音怎麼辦_win10藍芽音響連線成功沒聲音處理方法Win10藍芽
- 飛利浦SBT 300無線便攜藍芽音響 手雷式的音響藍芽
- iOS藍芽開發iOS藍芽
- win10藍芽音響已配對沒聲音怎麼辦_藍芽音響連線win10電腦沒聲音解決方法Win10藍芽
- Android 開啟藍芽流程Android藍芽
- 將Win10電腦變成藍芽音響Win10藍芽
- SOUNDBOKS藍芽音響:廣場舞大媽最愛藍芽
- BumpOut藍芽音響:可以貼在手機上音效完美藍芽
- win10臺式電腦如何連線藍芽音響_藍芽音響怎麼連線win10臺式電腦Win10藍芽
- 臺式電腦藍芽在哪裡開啟 臺式電腦怎麼連線藍芽耳機音響設定藍芽
- Android藍芽使用詳解(普通藍芽)Android藍芽
- 三星PK亞馬遜Echo 或推出語音互動藍芽音響亞馬遜藍芽
- Android 藍芽開發相關知識總結Android藍芽
- Android藍芽4.0(ble)開發的解決方案Android藍芽
- Android藍芽協議-藍芽掃描 startDiscoveryAndroid藍芽協議
- Win10系統聲音無法找到藍芽音響如何解決Win10藍芽
- iOS 藍芽開發 - swift版iOS藍芽Swift
- 微信小程式藍芽開發微信小程式藍芽
- JBL GO Smart 紅色 藍芽音響 開箱及使用體驗Go藍芽
- iOS藍芽開發 Bluetooth藍芽CoreBluetooth 藍芽中心裝置的實現 藍芽外設的實現 有DemoiOS藍芽
- Android BLE 藍芽開發——掃碼槍基於BLESSEDAndroid藍芽
- 藍芽音樂之歌詞同步藍芽
- 藍芽(Bluetooth)音訊協議藍芽音訊協議
- Android藍芽那點事——深入瞭解藍芽BlE藍芽 《總結篇》Android藍芽
- 保時捷零件製作的藍芽音響逼格甚高藍芽
- iOS 藍芽開發·基礎篇iOS藍芽