1.官方網址 https://developer.android.com/guide/topics/connectivity/bluetooth.html
2.步驟
首先,新增藍芽的許可權
<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
...
</manifest>
複製程式碼
2.1 開啟藍芽
第一種:引導使用者收到開啟
第二種:BLUETOOTH_ADMIN 可以用的mBluetoothAdapter.enable();
直接開啟
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 藍芽是否可用
if (!mBluetoothAdapter.isEnabled()) {
// 開啟藍芽
mBluetoothAdapter.enable();
}
// 關閉藍芽
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
}
複製程式碼
2.2 掃描附近可用的藍芽裝置 掃描到的結果會通過廣播的形式回撥,具體可以可以看附錄程式碼:
// 掃描藍芽裝置
mBluetoothAdapter.cancelDiscovery();
複製程式碼
2.3 連線接 2.4 傳送指令 主要是
附錄:
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
private ListView mLv;
private BluetoothAdapter mBluetoothAdapter;
private OutputStream mOutputStream;
private ArrayList<BluetoothDevice> mDevices = new ArrayList<BluetoothDevice>();
private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// 掃描到藍芽裝置
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDevices.add(device);
mAdapter.notifyDataSetChanged();
System.out.println("device name" + device.getName());
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Toast.makeText(getApplicationContext(), "開始掃描", Toast.LENGTH_SHORT).show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Toast.makeText(getApplicationContext(), "掃描結束", Toast.LENGTH_SHORT).show();
}
}
};
private DeviceAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLv = (ListView) findViewById(R.id.lv);
mAdapter = new DeviceAdapter(getApplicationContext(), mDevices);
mLv.setAdapter(mAdapter);
mLv.setOnItemClickListener(this);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// 註冊廣播接收者, 當掃描到藍芽裝置的時候, 系統會傳送廣播
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mBluetoothReceiver, filter);
}
public void clickBtn(View v) {
switch (v.getId()) {
case R.id.button1:
// 藍芽是否可用
if (!mBluetoothAdapter.isEnabled()) {
// 開啟藍芽
mBluetoothAdapter.enable();
}
break;
case R.id.button2:
// 關閉藍芽
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
}
break;
case R.id.button3:
// 開始掃描
mDevices.clear();
mAdapter.notifyDataSetChanged();
mBluetoothAdapter.startDiscovery();
break;
case R.id.button4:
// 停止掃描
mBluetoothAdapter.cancelDiscovery();
break;
case R.id.button5:
sendCtrl(0);
break;
case R.id.button6:
sendCtrl(1);
break;
case R.id.button7:
sendCtrl(2);
break;
default:
break;
}
}
private void sendCtrl(int i) {
try {
byte[] bs = new byte[5];
bs[0] = (byte)0x01;
bs[1] = (byte)0x99;
if(i== 0) {
bs[2] = (byte)0x10;
bs[3] = (byte)0x10;
}else if(i==1) {
bs[2] = (byte)0x11;
bs[3] = (byte)0x11;
}else if(i==2) {
bs[2] = (byte)0x17;
bs[3] = (byte)0x17;
}
bs[4] = (byte)0x99;
mOutputStream.write(bs);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mBluetoothReceiver);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BluetoothDevice device = mDevices.get(position);
conn(device);
}
private void conn(final BluetoothDevice device) {
// 建立藍芽連線是耗時操作, 類似TCP Socket, 需要放在子執行緒裡
new Thread() {
public void run() {
try {
// 獲取 BluetoothSocket, UUID需要和藍芽服務端保持一致
BluetoothSocket bluetoothSocket = device.createRfcommSocketToServiceRecord(UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB"));
// 和藍芽服務端建立連線
bluetoothSocket.connect();
// 獲取輸出流, 往藍芽服務端寫指令資訊
mOutputStream = bluetoothSocket.getOutputStream();
// 提示使用者
runOnUiThread(new Runnable() {
public void run() {
System.out.println("連線成功----");
Toast.makeText(getApplicationContext(), "連線成功", Toast.LENGTH_SHORT)
.show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}
}
複製程式碼