Android 蓝牙开发
Google中文开发文档
https://developer.android.google.cn/index.html
Android BLE 蓝牙开发
http://www.jianshu.com/p/72abf053640d
Android 蓝牙开发相关知识整理
http://www.jianshu.com/p/869e0f4409c5
Android 蓝牙开发中遇到的一些坑
http://www.jianshu.com/p/f7941371e1a7
Android经典蓝牙开发简介
http://www.jianshu.com/p/fc46c154eb77
public class MainActivity extends Activity implements OnItemClickListener {
private ListView mLv;
private BluetoothAdapter mBluetoothAdapter;
private OutputStream mOutputStream;
private ArrayList<BluetoothDevice> mDevices = new ArrayList<BluetoothDevice>();
private DeviceAdapter mAdapter;
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 (Blu7页etoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Toast.makeText(getApplicationContext(), "扫描结束", Toast.LENGTH_SHORT).show();
}
}
};
@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();
}
}
public class DeviceAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mDevices;
private Context mContext;
public DeviceAdapter(Context context, ArrayList<BluetoothDevice> devices) {
mDevices = devices;
mContext = context;
}
@Override
public int getCount() {
return mDevices.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = View.inflate(mContext, R.layout.item, null);
holder = new ViewHolder();
holder.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
holder.mTvAddress = (TextView) convertView.findViewById(R.id.tv_address);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
BluetoothDevice device = mDevices.get(position);
holder.mTvName.setText(device.getName());
holder.mTvAddress.setText(device.getAddress());
return convertView;
}
class ViewHolder {
TextView mTvName;
TextView mTvAddress;
}
}