Android蓝牙通信具体解释
蓝牙通信的大概过程例如以下:
1。首先开启蓝牙
2,搜索可用设备
3,创建蓝牙socket。获取输入输出流
4,读取和写入数据
5。断开连接关闭蓝牙
还要发送配对码发送进行推断!
以下是全部的源码;不会非常难;认真看;
SearchDeviceActivity.java
- package com.hello.project;
 - import java.util.ArrayList;
 - import java.util.Iterator;
 - import java.util.List;
 - import java.util.Set;
 - import android.app.Activity;
 - import android.app.AlertDialog;
 - import android.bluetooth.BluetoothAdapter;
 - import android.bluetooth.BluetoothDevice;
 - import android.content.BroadcastReceiver;
 - import android.content.Context;
 - import android.content.DialogInterface;
 - import android.content.Intent;
 - import android.content.IntentFilter;
 - import android.os.Bundle;
 - import android.util.Log;
 - import android.view.View;
 - import android.view.View.OnClickListener;
 - import android.widget.AdapterView;
 - import android.widget.AdapterView.OnItemClickListener;
 - import android.widget.ArrayAdapter;
 - import android.widget.Button;
 - import android.widget.ListView;
 - public class SearchDeviceActivity extends Activity implements OnItemClickListener{
 - private BluetoothAdapter blueadapter=null;
 - private DeviceReceiver mydevice=new DeviceReceiver();
 - private List<String> deviceList=new ArrayList<String>();
 - private ListView deviceListview;
 - private Button btserch;
 - private ArrayAdapter<String> adapter;
 - private boolean hasregister=false;
 - @Override
 - protected void onCreate(Bundle savedInstanceState) {
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.finddevice);
 - setView();
 - setBluetooth();
 - }
 - private void setView(){
 - deviceListview=(ListView)findViewById(R.id.devicelist);
 - adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, deviceList);
 - deviceListview.setAdapter(adapter);
 - deviceListview.setOnItemClickListener(this);
 - btserch=(Button)findViewById(R.id.start_seach);
 - btserch.setOnClickListener(new ClinckMonitor());
 - }
 - @Override
 - protected void onStart() {
 - //注冊蓝牙接收广播
 - if(!hasregister){
 - hasregister=true;
 - IntentFilter filterStart=new IntentFilter(BluetoothDevice.ACTION_FOUND);
 - IntentFilter filterEnd=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
 - registerReceiver(mydevice, filterStart);
 - registerReceiver(mydevice, filterEnd);
 - }
 - super.onStart();
 - }
 - @Override
 - protected void onDestroy() {
 - if(blueadapter!=null&&blueadapter.isDiscovering()){
 - blueadapter.cancelDiscovery();
 - }
 - if(hasregister){
 - hasregister=false;
 - unregisterReceiver(mydevice);
 - }
 - super.onDestroy();
 - }
 - /**
 - * Setting Up Bluetooth
 - */
 - private void setBluetooth(){
 - blueadapter=BluetoothAdapter.getDefaultAdapter();
 - if(blueadapter!=null){ //Device support Bluetooth
 - //确认开启蓝牙
 - if(!blueadapter.isEnabled()){
 - //请求用户开启
 - Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 - startActivityForResult(intent, RESULT_FIRST_USER);
 - //使蓝牙设备可见,方便配对
 - Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
 - in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
 - startActivity(in);
 - //直接开启,不经过提示
 - blueadapter.enable();
 - }
 - }
 - else{ //Device does not support Bluetooth
 - AlertDialog.Builder dialog = new AlertDialog.Builder(this);
 - dialog.setTitle("No bluetooth devices");
 - dialog.setMessage("Your equipment does not support bluetooth, please change device");
 - dialog.setNegativeButton("cancel",
 - new DialogInterface.OnClickListener() {
 - @Override
 - public void onClick(DialogInterface dialog, int which) {
 - }
 - });
 - dialog.show();
 - }
 - }
 - /**
 - * Finding Devices
 - */
 - private void findAvalibleDevice(){
 - //获取可配对蓝牙设备
 - Set<BluetoothDevice> device=blueadapter.getBondedDevices();
 - if(blueadapter!=null&&blueadapter.isDiscovering()){
 - deviceList.clear();
 - adapter.notifyDataSetChanged();
 - }
 - if(device.size()>0){ //存在已经配对过的蓝牙设备
 - for(Iterator<BluetoothDevice> it=device.iterator();it.hasNext();){
 - BluetoothDevice btd=it.next();
 - deviceList.add(btd.getName()+'\n'+btd.getAddress());
 - adapter.notifyDataSetChanged();
 - }
 - }else{ //不存在已经配对过的蓝牙设备
 - deviceList.add("No can be matched to use bluetooth");
 - adapter.notifyDataSetChanged();
 - }
 - }
 - @Override
 - protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 - switch(resultCode){
 - case RESULT_OK:
 - findAvalibleDevice();
 - break;
 - case RESULT_CANCELED:
 - break;
 - }
 - super.onActivityResult(requestCode, resultCode, data);
 - }
 - private class ClinckMonitor implements OnClickListener{
 - @Override
 - public void onClick(View v) {
 - if(blueadapter.isDiscovering()){
 - blueadapter.cancelDiscovery();
 - btserch.setText("repeat search");
 - }else{
 - findAvalibleDevice();
 - blueadapter.startDiscovery();
 - btserch.setText("stop search");
 - }
 - }
 - }
 - /**
 - * 蓝牙搜索状态广播监听
 - * @author Andy
 - *
 - */
 - private class DeviceReceiver extends BroadcastReceiver{
 - @Override
 - public void onReceive(Context context, Intent intent) {
 - String action =intent.getAction();
 - if(BluetoothDevice.ACTION_FOUND.equals(action)){ //搜索到新设备
 - BluetoothDevice btd=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 - //搜索没有配过对的蓝牙设备
 - if (btd.getBondState() != BluetoothDevice.BOND_BONDED) {
 - deviceList.add(btd.getName()+'\n'+btd.getAddress());
 - adapter.notifyDataSetChanged();
 - }
 - }
 - else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ //搜索结束
 - if (deviceListview.getCount() == 0) {
 - deviceList.add("No can be matched to use bluetooth");
 - adapter.notifyDataSetChanged();
 - }
 - btserch.setText("repeat search");
 - }
 - }
 - }
 - @Override
 - public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
 - Log.e("msgParent", "Parent= "+arg0);
 - Log.e("msgView", "View= "+arg1);
 - Log.e("msgChildView", "ChildView= "+arg0.getChildAt(pos-arg0.getFirstVisiblePosition()));
 - final String msg = deviceList.get(pos);
 - if(blueadapter!=null&&blueadapter.isDiscovering()){
 - blueadapter.cancelDiscovery();
 - btserch.setText("repeat search");
 - }
 - AlertDialog.Builder dialog = new AlertDialog.Builder(this);// 定义一个弹出框对象
 - dialog.setTitle("Confirmed connecting device");
 - dialog.setMessage(msg);
 - dialog.setPositiveButton("connect",
 - new DialogInterface.OnClickListener() {
 - @Override
 - public void onClick(DialogInterface dialog, int which) {
 - BluetoothMsg.BlueToothAddress=msg.substring(msg.length()-17);
 - if(BluetoothMsg.lastblueToothAddress!=BluetoothMsg.BlueToothAddress){
 - BluetoothMsg.lastblueToothAddress=BluetoothMsg.BlueToothAddress;
 - }
 - Intent in=new Intent(SearchDeviceActivity.this,BluetoothActivity.class);
 - startActivity(in);
 - }
 - });
 - dialog.setNegativeButton("cancel",
 - new DialogInterface.OnClickListener() {
 - @Override
 - public void onClick(DialogInterface dialog, int which) {
 - BluetoothMsg.BlueToothAddress = null;
 - }
 - });
 - dialog.show();
 - }
 - }
 
BluetoothMsg.java
- package com.hello.project;
 - public class BluetoothMsg {
 - /**
 - * 蓝牙连接类型
 - * @author Andy
 - *
 - */
 - public enum ServerOrCilent{
 - NONE,
 - SERVICE,
 - CILENT
 - };
 - //蓝牙连接方式
 - public static ServerOrCilent serviceOrCilent = ServerOrCilent.NONE;
 - //连接蓝牙地址
 - public static String BlueToothAddress = null,lastblueToothAddress=null;
 - //通信线程是否开启
 - public static boolean isOpen = false;
 - }
 
finddevice.xml
- <?xml version="1.0" encoding="utf-8"?>
 - <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 - android:id = "@+id/devices"
 - android:orientation="vertical"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - >
 - <RelativeLayout
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:layout_alignParentBottom = "true"
 - android:id= "@+id/bt_bottombar">
 - <Button android:id="@+id/start_seach"
 - android:layout_width="match_parent"
 - android:layout_height="wrap_content"
 - android:layout_toRightOf="@+id/start_service"
 - android:text="Began to search"/>
 - </RelativeLayout>
 - <ListView
 - android:id="@+id/devicelist"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - android:scrollingCache="false"
 - android:divider="#ffc6c6c6"
 - android:layout_weight="1.0"
 - android:layout_above = "@id/bt_bottombar"
 - android:layout_below="@id/devices"
 - />
 - </RelativeLayout>
 
BluetoothActivity.java
- package com.hello.project;
 - import java.io.IOException;
 - import java.io.InputStream;
 - import java.io.OutputStream;
 - import java.util.ArrayList;
 - import java.util.List;
 - import java.util.UUID;
 - import android.app.Activity;
 - import android.bluetooth.BluetoothAdapter;
 - import android.bluetooth.BluetoothDevice;
 - import android.bluetooth.BluetoothServerSocket;
 - import android.bluetooth.BluetoothSocket;
 - import android.content.Context;
 - import android.os.Bundle;
 - import android.os.Handler;
 - import android.os.Message;
 - import android.util.Log;
 - import android.view.View;
 - import android.view.View.OnClickListener;
 - import android.view.inputmethod.InputMethodManager;
 - import android.widget.AdapterView;
 - import android.widget.ArrayAdapter;
 - import android.widget.Button;
 - import android.widget.EditText;
 - import android.widget.ListView;
 - import android.widget.Toast;
 - import android.widget.AdapterView.OnItemClickListener;
 - public class BluetoothActivity extends Activity{
 - /* 一些常量。代表server的名称 */
 - public static final String PROTOCOL_SCHEME_RFCOMM = "btspp";
 - private ListView mListView;
 - private Button sendButton;
 - private Button disconnectButton;
 - private EditText editMsgView;
 - private ArrayAdapter<String> mAdapter;
 - private List<String> msgList=new ArrayList<String>();
 - Context mContext;
 - private BluetoothServerSocket mserverSocket = null;
 - private ServerThread startServerThread = null;
 - private clientThread clientConnectThread = null;
 - private BluetoothSocket socket = null;
 - private BluetoothDevice device = null;
 - private readThread mreadThread = null;;
 - private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 - @Override
 - public void onCreate(Bundle savedInstanceState) {
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.chat);
 - mContext = this;
 - init();
 - }
 - private void init() {
 - mAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, msgList);
 - mListView = (ListView) findViewById(R.id.list);
 - mListView.setAdapter(mAdapter);
 - mListView.setFastScrollEnabled(true);
 - editMsgView= (EditText)findViewById(R.id.MessageText);
 - editMsgView.clearFocus();
 - sendButton= (Button)findViewById(R.id.btn_msg_send);
 - sendButton.setOnClickListener(new OnClickListener() {
 - @Override
 - public void onClick(View arg0) {
 - String msgText =editMsgView.getText().toString();
 - if (msgText.length()>0) {
 - sendMessageHandle(msgText);
 - editMsgView.setText("");
 - editMsgView.clearFocus();
 - //close InputMethodManager
 - InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 - imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);
 - }else
 - Toast.makeText(mContext, "发送内容不能为空!", Toast.LENGTH_SHORT).show();
 - }
 - });
 - disconnectButton= (Button)findViewById(R.id.btn_disconnect);
 - disconnectButton.setOnClickListener(new OnClickListener() {
 - @Override
 - public void onClick(View arg0) {
 - // TODO Auto-generated method stub
 - if (BluetoothMsg.serviceOrCilent == BluetoothMsg.ServerOrCilent.CILENT)
 - {
 - shutdownClient();
 - }
 - else if (BluetoothMsg.serviceOrCilent == BluetoothMsg.ServerOrCilent.SERVICE)
 - {
 - shutdownServer();
 - }
 - BluetoothMsg.isOpen = false;
 - BluetoothMsg.serviceOrCilent=BluetoothMsg.ServerOrCilent.NONE;
 - Toast.makeText(mContext, "已断开连接。", Toast.LENGTH_SHORT).show();
 - }
 - });
 - }
 - private Handler LinkDetectedHandler = new Handler() {
 - @Override
 - public void handleMessage(Message msg) {
 - //Toast.makeText(mContext, (String)msg.obj, Toast.LENGTH_SHORT).show();
 - if(msg.what==1)
 - {
 - msgList.add((String)msg.obj);
 - }
 - else
 - {
 - msgList.add((String)msg.obj);
 - }
 - mAdapter.notifyDataSetChanged();
 - mListView.setSelection(msgList.size() - 1);
 - }
 - };
 - @Override
 - protected void onResume() {
 - BluetoothMsg.serviceOrCilent=BluetoothMsg.ServerOrCilent.CILENT;
 - if(BluetoothMsg.isOpen)
 - {
 - Toast.makeText(mContext, "连接已经打开。能够通信。假设要再建立连接,请先断开!
", Toast.LENGTH_SHORT).show();
 - return;
 - }
 - if(BluetoothMsg.serviceOrCilent==BluetoothMsg.ServerOrCilent.CILENT)
 - {
 - String address = BluetoothMsg.BlueToothAddress;
 - if(!address.equals("null"))
 - {
 - device = mBluetoothAdapter.getRemoteDevice(address);
 - clientConnectThread = new clientThread();
 - clientConnectThread.start();
 - BluetoothMsg.isOpen = true;
 - }
 - else
 - {
 - Toast.makeText(mContext, "address is null !", Toast.LENGTH_SHORT).show();
 - }
 - }
 - else if(BluetoothMsg.serviceOrCilent==BluetoothMsg.ServerOrCilent.SERVICE)
 - {
 - startServerThread = new ServerThread();
 - startServerThread.start();
 - BluetoothMsg.isOpen = true;
 - }
 - super.onResume();
 - }
 - //开启client
 - private class clientThread extends Thread {
 - @Override
 - public void run() {
 - try {
 - //创建一个Socket连接:仅仅须要server在注冊时的UUID号
 - // socket = device.createRfcommSocketToServiceRecord(BluetoothProtocols.OBEX_OBJECT_PUSH_PROTOCOL_UUID);
 - socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
 - //连接
 - Message msg2 = new Message();
 - msg2.obj = "请稍候。正在连接server:"+BluetoothMsg.BlueToothAddress;
 - msg2.what = 0;
 - LinkDetectedHandler.sendMessage(msg2);
 - socket.connect();
 - Message msg = new Message();
 - msg.obj = "已经连接上服务端。能够发送信息。";
 - msg.what = 0;
 - LinkDetectedHandler.sendMessage(msg);
 - //启动接受数据
 - mreadThread = new readThread();
 - mreadThread.start();
 - }
 - catch (IOException e)
 - {
 - Log.e("connect", "", e);
 - Message msg = new Message();
 - msg.obj = "连接服务端异常!断开连接又一次试一试。";
 - msg.what = 0;
 - LinkDetectedHandler.sendMessage(msg);
 - }
 - }
 - };
 - //开启server
 - private class ServerThread extends Thread {
 - @Override
 - public void run() {
 - try {
 - /* 创建一个蓝牙server
 - * 參数分别:server名称、UUID */
 - mserverSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(PROTOCOL_SCHEME_RFCOMM,
 - UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
 - Log.d("server", "wait cilent connect...");
 - Message msg = new Message();
 - msg.obj = "请稍候。正在等待client的连接...";
 - msg.what = 0;
 - LinkDetectedHandler.sendMessage(msg);
 - /* 接受client的连接请求 */
 - socket = mserverSocket.accept();
 - Log.d("server", "accept success !");
 - Message msg2 = new Message();
 - String info = "client已经连接上!能够发送信息。";
 - msg2.obj = info;
 - msg.what = 0;
 - LinkDetectedHandler.sendMessage(msg2);
 - //启动接受数据
 - mreadThread = new readThread();
 - mreadThread.start();
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - }
 - };
 - /* 停止server */
 - private void shutdownServer() {
 - new Thread() {
 - @Override
 - public void run() {
 - if(startServerThread != null)
 - {
 - startServerThread.interrupt();
 - startServerThread = null;
 - }
 - if(mreadThread != null)
 - {
 - mreadThread.interrupt();
 - mreadThread = null;
 - }
 - try {
 - if(socket != null)
 - {
 - socket.close();
 - socket = null;
 - }
 - if (mserverSocket != null)
 - {
 - mserverSocket.close();/* 关闭服务器 */
 - mserverSocket = null;
 - }
 - } catch (IOException e) {
 - Log.e("server", "mserverSocket.close()", e);
 - }
 - };
 - }.start();
 - }
 - /* 停止client连接 */
 - private void shutdownClient() {
 - new Thread() {
 - @Override
 - public void run() {
 - if(clientConnectThread!=null)
 - {
 - clientConnectThread.interrupt();
 - clientConnectThread= null;
 - }
 - if(mreadThread != null)
 - {
 - mreadThread.interrupt();
 - mreadThread = null;
 - }
 - if (socket != null) {
 - try {
 - socket.close();
 - } catch (IOException e) {
 - // TODO Auto-generated catch block
 - e.printStackTrace();
 - }
 - socket = null;
 - }
 - };
 - }.start();
 - }
 - //发送数据
 - private void sendMessageHandle(String msg)
 - {
 - if (socket == null)
 - {
 - Toast.makeText(mContext, "没有连接", Toast.LENGTH_SHORT).show();
 - return;
 - }
 - try {
 - OutputStream os = socket.getOutputStream();
 - os.write(msg.getBytes());
 - } catch (IOException e) {
 - e.printStackTrace();
 - }
 - msgList.add(msg);
 - mAdapter.notifyDataSetChanged();
 - mListView.setSelection(msgList.size() - 1);
 - }
 - //读取数据
 - private class readThread extends Thread {
 - @Override
 - public void run() {
 - byte[] buffer = new byte[1024];
 - int bytes;
 - InputStream mmInStream = null;
 - try {
 - mmInStream = socket.getInputStream();
 - } catch (IOException e1) {
 - // TODO Auto-generated catch block
 - e1.printStackTrace();
 - }
 - while (true) {
 - try {
 - // Read from the InputStream
 - if( (bytes = mmInStream.read(buffer)) > 0 )
 - {
 - byte[] buf_data = new byte[bytes];
 - for(int i=0; i<bytes; i++)
 - {
 - buf_data[i] = buffer[i];
 - }
 - String s = new String(buf_data);
 - Message msg = new Message();
 - msg.obj = s;
 - msg.what = 1;
 - LinkDetectedHandler.sendMessage(msg);
 - }
 - } catch (IOException e) {
 - try {
 - mmInStream.close();
 - } catch (IOException e1) {
 - // TODO Auto-generated catch block
 - e1.printStackTrace();
 - }
 - break;
 - }
 - }
 - }
 - }
 - @Override
 - protected void onDestroy() {
 - super.onDestroy();
 - if (BluetoothMsg.serviceOrCilent == BluetoothMsg.ServerOrCilent.CILENT)
 - {
 - shutdownClient();
 - }
 - else if (BluetoothMsg.serviceOrCilent == BluetoothMsg.ServerOrCilent.SERVICE)
 - {
 - shutdownServer();
 - }
 - BluetoothMsg.isOpen = false;
 - BluetoothMsg.serviceOrCilent = BluetoothMsg.ServerOrCilent.NONE;
 - }
 - }
 
chat.xml
- <?xml version="1.0" encoding="utf-8"?
>
 - <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 - android:id = "@+id/container"
 - android:orientation="vertical"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - >
 - <RelativeLayout
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:id= "@+id/edit_bottombar"
 - android:layout_alignParentBottom = "true">
 - <Button android:id="@+id/btn_disconnect"
 - android:layout_width="65dp"
 - android:layout_height="wrap_content"
 - android:layout_alignParentLeft ="true"
 - android:text="断开"/>
 - <Button android:id="@+id/btn_msg_send"
 - android:layout_width="65dp"
 - android:layout_height="wrap_content"
 - android:layout_alignParentRight ="true"
 - android:text="发送"/>
 - <EditText
 - android:layout_width="fill_parent"
 - android:layout_height = "wrap_content"
 - android:layout_toLeftOf="@id/btn_msg_send"
 - android:layout_toRightOf="@+id/btn_disconnect"
 - android:hint = "说点什么呢?"
 - android:textSize="15dip"
 - android:id = "@+id/MessageText"/>
 - </RelativeLayout>
 - <ListView
 - android:id="@+id/list"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - android:scrollingCache="false"
 - android:divider="#ffc6c6c6"
 - android:layout_weight="1.0"
 - android:layout_above = "@id/edit_bottombar"
 - android:layout_below="@id/container"
 - />
 - </RelativeLayout>
 
最后别忘了增加权限
- <uses-permission android:name="android.permission.BLUETOOTH"/>
 - <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
 - <uses-permission android:name="android.permission.READ_CONTACTS"/>
 
扩展:蓝牙后台配对实现(网上看到的整理例如以下)
- static public boolean createBond(Class btClass, BluetoothDevice btDevice)
 - throws Exception {
 - Method createBondMethod = btClass.getMethod("createBond");
 - Log.i("life", "createBondMethod = " + createBondMethod.getName());
 - Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
 - return returnValue.booleanValue();
 - }
 - static public boolean setPin(Class btClass, BluetoothDevice btDevice,
 - String str) throws Exception {
 - Boolean returnValue = null;
 - try {
 - Method removeBondMethod = btClass.getDeclaredMethod("setPin",
 - new Class[] { byte[].class });
 - returnValue = (Boolean) removeBondMethod.invoke(btDevice,
 - new Object[] { str.getBytes() });
 - Log.i("life", "returnValue = " + returnValue);
 - } catch (SecurityException e) {
 - // throw new RuntimeException(e.getMessage());
 - e.printStackTrace();
 - } catch (IllegalArgumentException e) {
 - // throw new RuntimeException(e.getMessage());
 - e.printStackTrace();
 - } catch (Exception e) {
 - // TODO Auto-generated catch block
 - e.printStackTrace();
 - }
 - return returnValue;
 - }
 - // 取消用户输入
 - static public boolean cancelPairingUserInput(Class btClass,
 - BluetoothDevice device) throws Exception {
 - Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
 - // cancelBondProcess()
 - Boolean returnValue = (Boolean) createBondMethod.invoke(device);
 - Log.i("life", "cancelPairingUserInputreturnValue = " + returnValue);
 - return returnValue.booleanValue();
 - }
 
然后监听蓝牙配对的广播  匹配“android.bluetooth.device.action.PAIRING_REQUEST”这个action
然后调用上面的setPin(mDevice.getClass(), mDevice, "1234"); // 手机和蓝牙採集器配对
createBond(mDevice.getClass(), mDevice);
cancelPairingUserInput(mDevice.getClass(), mDevice);
mDevice是你要去连接的那个蓝牙的对象 , 1234为配对的pin码
Android蓝牙通信具体解释的更多相关文章
- Android蓝牙通信总结
		
这篇文章要达到的目标: 1.介绍在Android系统上实现蓝牙通信的过程中涉及到的概念. 2.在android系统上实现蓝牙通信的步骤. 3.在代码实现上的考虑. 4.例子代码实现(手持设备和蓝牙串口 ...
 - Qt on Android 蓝牙通信开发
		
版权声明:本文为MULTIBEANS ORG研发跟随文章,未经MLT ORG允许不得转载. 最近做项目,需要开发安卓应用,实现串口的收发,目测CH340G在安卓手机上非常麻烦,而且驱动都是Java版本 ...
 - Android蓝牙通信
		
Android为蓝牙设备之间的通信封装好了一些调用接口,使得实现Android的蓝牙通信功能并不困难.可通过UUID使两个设备直接建立连接. 具体步骤: 1. 获取BluetoothAdapter实例 ...
 - android蓝牙协议名词解释 OPP HFP HDP A2DP PAN
		
各种蓝牙协议的全称: OPP:对象存储规范(Object Push Profile),最为常见的,文件的传输都是使用此协议. HFP:(Hands-free Profile),让蓝牙设备能够控制电话, ...
 - (转)android 蓝牙通信编程
		
转自:http://blog.csdn.net/pwei007/article/details/6015907 Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输. 本文档描述了怎样 ...
 - Android蓝牙通信功能开发
		
1. 概述 Bluetooth 是几乎现在每部手机标准配备的功能,多用于耳机 mic 等设备与手机的连接,除此之外,还可以多部手机之间建立 bluetooth 通信,本文就通过 SDK 中带的一个聊天 ...
 - android 蓝牙通信编程讲解
		
以下是开发中的几个关键步骤: 1,首先开启蓝牙 2,搜索可用设备 3,创建蓝牙socket,获取输入输出流 4,读取和写入数据 5,断开连接关闭蓝牙 下面是一个demo 效果图: SearchDevi ...
 - android 蓝牙 通信 bluetooth
		
此例子基于 android demo Android的蓝牙开发,虽然不多用,但有时还是会用到, Android对于蓝牙开发从2.0版本的sdk才开始支持,而且模拟器不支持,测试需要两部手机: ...
 - Android 蓝牙通信——AndroidBluetoothManager
		
转载请说明出处! 作者:kqw攻城狮 出处:个人站 | CSDN To get a Git project into your build: Step 1. Add the JitPack repos ...
 
随机推荐
- js延时函数setTimeout
			
实现一个延时执行的效果,现记录如下: <html> <head> <script type="text/javascript" src="/ ...
 - appium 测试模拟器时输入adb devices显示 unauthorized
			
https://stackoverflow.com/questions/32132434/set-adb-vendor-keys 也就是点击AVD管理器右边的下拉列表,点击清除数据,再重启虚拟机 0d ...
 - Makefile学习之一
			
Makefile注意: 1.Makefile由三部分组成:目标,依赖,命令: 2.命令行前必须有一个tab键作为开头: 3.定义变量:objects=main.o abc.o 使用$(objects) ...
 - [React] Use react-rewards to add microinteractions to React app to reward users for some actions
			
It's important that our users enjoy using our application or website. One way we can make it happen ...
 - 使用终端shell命令批量改动一个文件下的全部文件的读写权限
			
之前对openfire安装的目录就遇到过这个问题,今天再次遇到.须要改动一个目录以下的全部子目录以及文件的三个权限:本用户读写.管理员读写.全部人读写,三个都要需改为wr 步骤例如以下:比如我要改动/ ...
 - webstrom 中 plugins error 设置里 Languages & Frameworks里面没有JavaScript?
			
不知道哪里不对 js突然不支持高亮 于是在设置里面找language & frameworks里面的JavaScript 选项 但是竟然没有JavaScript选项. 还有plugins er ...
 - C++ 大规模数据排序(100G数据 使用 4G 内存 排序)
			
思路很简单,先分段排序,存储到临时文件中,然后合并. 使用10000个整数来模拟大数据,每次读取100个到内存中. #include <stdint.h> #include <std ...
 - vscode - emmet失效?
			
把emmet设置覆盖为用户.
 - axios 设置超时时间 timeout
			
this.$ajax.post('', {operate: type, ids: this.data.id.toString(), data_type: 'ips'}, {timeout: 60000 ...
 - axios 处理并发请求
			
//同时发起多个请求时的处理 axios.all([get1(), get2()]) .then(axios.spread(function (res1, res2) { // 只有两个请求都完成才会 ...