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();
}
}