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 ...
随机推荐
- A system tap script to detect UDP beacons
https://gist.github.com/jbradley89/178bbf3944786c494bd78f3df16a5472
- Chrome的开发必备小技巧
谷歌Chrome,是当前最流行且被众多web开发人员使用的浏览器.最快六周就更新发布一次以及伴随着它不断强大的开发组件,使得Chrome成为你必备的开发工具.例如,在线编辑CSS,console以及d ...
- android开发之自定义圆形ImagView
在日常使用中我们经常会使用到圆形的图片,但是android系统中并没有默认的圆形控件,所以我们需要自己来写一个自定义的ImagView来显示一张圆形的图片,下面先看效果 详细的方法是我们自定义一个类, ...
- #if 条件编译
1.格式: #if constant-expression statements #elif constant-expression statements #else statements #endi ...
- ElasticSearch 排序
1.相关性排序 ElasticSearch为了按照相关性来排序,需要将相关性表示为一个数值,在 Elasticsearch 中, 相关性得分 由一个浮点数进行表示,并在搜索结果中通过 _score 参 ...
- Python 最火 IDE 最受欢迎(转载)
来自:开源中国社区 链接:https://www.oschina.net/news/86973/packt-skill-up-2017 电子书网站 Packt 刚刚发布了第三届 “Skill UP” ...
- ELKStack日志离线系统
通过Filebeat抽取数据到logstash中,转存到ElasticSearch中,最后通过Kibana进行展示 https://www.ibm.com/developerworks/cn/open ...
- [转]js模块化编程之彻底弄懂CommonJS和AMD/CMD!
原文: https://www.cnblogs.com/chenguangliang/p/5856701.html ------------------------------------------ ...
- 倍福TwinCAT(贝福Beckhoff)基础教程 松下伺服驱动器报错 24.0怎么办
24.0 位置偏差过大保护 读取驱动器参数之后,在基本的014项目把设定值设置为最大,然后点击传送,EEP写入驱动器后重启驱动器即可 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空 ...
- DNS 取得授权
1.阿里云上cnroot.cn申请DNS解析服务器 也就是cnroot.cn下的子域名都从这个DNS上获取. 如www.cnroot.cn 如 handle.cnroot.cn 2.vi /home/ ...