Android蓝牙技术+Demo
蓝牙技术
打开和关闭蓝牙设备:
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent,1);
<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); //调用enable就把蓝牙打开 adapter.enable(); //调用disable就把蓝牙关闭 adapter.disable();
蓝牙数据传输:
package com.sadaharu.jacksom.bluetooth; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothServerSocket; 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.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.UUID; public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{private BluetoothAdapter mBluetoothAdapter; private ListView lvDevices; private List<String> bluetoothDevices = new ArrayList<>(); private ArrayAdapter<String> arrayAdapter; private final UUID MY_UUID = UUID.randomUUID(); private final String NAME = "Bluetooth_Socket"; private BluetoothSocket clientSocket; private BluetoothDevice device; private AcceptThread acceptThread; private OutputStream os; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*//蓝牙的打开方式一: Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent,1); //蓝牙的打开方式二: BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); //调用enable就把蓝牙打开 adapter.enable(); //调用disable就把蓝牙关闭 adapter.disable();*/ lvDevices = (ListView) findViewById(R.id.lvDevices); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); acceptThread = new AcceptThread(); acceptThread.start(); //已经配过对的,存储在这个集合里 Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); //将已经配对过的设备显示出来 if (pairedDevices.size() > 0){for (BluetoothDevice device : pairedDevices){bluetoothDevices.add(device.getName() + ":" + device.getAddress()); }}arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,bluetoothDevices); lvDevices.setAdapter(arrayAdapter); lvDevices.setOnItemClickListener(this); //找到一个设备会发送广播 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); this.registerReceiver(receiver, filter); //全部搜索完成会发送一个广播 filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(receiver, filter); }//按钮监听方法 public void onClick_Search(View view){//设置一个进度条显示正在扫描 setProgressBarVisibility(true); setTitle("正在扫描……"); if (mBluetoothAdapter.isDiscovering()){mBluetoothAdapter.cancelDiscovery(); }mBluetoothAdapter.startDiscovery(); }private final BroadcastReceiver receiver = 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); //如果没有被配对过,再添加 if (device.getBondState() != BluetoothDevice.BOND_BONDED){bluetoothDevices.add(device.getName() + ":" + device.getAddress() + "\n"); arrayAdapter.notifyDataSetChanged(); }}else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){setProgressBarVisibility(false); setTitle("搜索已经完成"); }}}; @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {String s = arrayAdapter.getItem(position); String address = s.substring(s.indexOf(":") + 1).trim(); try{if (mBluetoothAdapter.isDiscovering()){mBluetoothAdapter.cancelDiscovery(); }if (device == null){device = mBluetoothAdapter.getRemoteDevice(address); }if (clientSocket == null){clientSocket = device.createRfcommSocketToServiceRecord(MY_UUID); clientSocket.connect(); os = clientSocket.getOutputStream(); }}catch (Exception e ){}if (os != null){try {os.write("发送信息到其他蓝牙设备".getBytes("utf-8")); } catch (IOException e) {e.printStackTrace(); }}}//服务端 private Handler handler = new Handler(){public void handlerMessage(Message msg){Toast.makeText(MainActivity.this,String.valueOf(msg.obj),Toast.LENGTH_LONG).show(); super.handleMessage(msg); }}; private class AcceptThread extends Thread{private BluetoothServerSocket serverSocket; private BluetoothSocket socket; private InputStream is; private OutputStream os; public AcceptThread(){try {serverSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME,MY_UUID); } catch (IOException e) {e.printStackTrace(); }}@Override public void run() {try {socket = serverSocket.accept(); is = socket.getInputStream(); os = socket.getOutputStream(); //有数据就从客户端读入,没有数据就阻塞 while (true){byte[] buffer = new byte[128]; int count = is.read(buffer); Message msg = new Message(); msg.obj = new String(buffer, 0,count,"utf-8"); handler.sendMessage(msg); }} catch (IOException e) {e.printStackTrace(); }}} }
Android蓝牙技术+Demo相关推荐
- Android 蓝牙技术(一)- 经典蓝牙
Android平台支持蓝牙设备通过蓝牙协议栈来实现无线通信.应用层提供了Bluetooth APIs来使用蓝牙功能, 这些API可以使应用可以无线连接其他蓝牙设备实现点对点及多点间通信. 使用Blue ...
- Android 蓝牙技术 实现终端间数据传输
蓝牙技术在智能硬件方面有很多用武之地,今天我就为大家分享一下蓝牙技术在Android系统下的使用方法技巧.蓝牙是一种短距离的无线通信技术标准,蓝牙协议分为4层,即核心协议层.电缆替代协议层.电话控制协 ...
- Android BLE 低功耗蓝牙技术使用解析
什么是BLE BLE 全称为 Bluetooth low energy,意思是低功耗蓝牙. Android 4.3 (API 18)之后引入Ble. 最常用的Ble蓝牙技术是设备之间传输数据. And ...
- 【转载】Android蓝牙自动配对Demo
注:新版本安卓需增加权限 <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION ...
- Android中的蓝牙技术
随着智能化生活的发展,手机成为人们生活的必需品,而蓝牙技术也随之应运而生.蓝牙技术作为现代移动设备与设备之间传输数据的一种主流方式,已经广泛应用于手表.耳机.车载系统等多种设备.在Android设备中 ...
- Android 蓝牙4.0(BLE)Demo
给大家分享一下蓝牙4.0的Demo,本篇介绍基本使用 最终效果 如何获取UUID 使用 Android 蓝牙4.0(BLE) Demo下载 最终效果 如何获取UUID 去手机华为应用市场下载一个叫蓝牙 ...
- Android BLE(低功耗蓝牙)技术总结
文章目录 前言 一.蓝牙介绍 1.什么是蓝牙? 2.蓝牙版本介绍 二.低功耗蓝牙(BLE) 1.BLE介绍 2.经典蓝牙(Classic Bluetooth)与低功耗蓝牙(BLE)的区别 3.低功耗蓝 ...
- android 蓝牙低耗能(LBE)技术介绍
蓝牙低能耗(BLE)技术是低成本.短距离.可互操作的鲁棒性无线技术.工作在免许可的2.4GHz ISM射频频段.它从一開始就设计为超低功耗(ULP)无线技术. 它利用很多智能手段最大限度地减少功耗. ...
- Android的BLE广播数据包介绍和解析---BLE--Android系列, 蓝牙技术BLE
目录 一.引言 二.广播的类型 三.广播数据格式 四.广播数据解析 五.总结 一.引言 理解和分析这个数据包结构(这里面也涉及广播间隔时间的设置,设备广播数据间隔设置长了,会影响设备被发现的效率:设置 ...
最新文章
- 【廖雪峰python入门笔记】list_按照索引访问
- 关于CSS中的字体尺寸设置 em rem等
- 数字语音信号处理学习笔记——语音信号的数字模型(3)
- PS制作立体效果——圆柱
- 开课啦 dubbo-go 微服务升级实战
- 结构型设计模式在公司项目中的运用实践
- 2019-2020-1 20175313 《信息安全系统设计基础》第二周学习总结
- ribbon基于接口配置超时_Spring Cloud Ribbon配置详解
- 点云sift matlab,点云配准SIFT算法
- 江苏省2021年高考成绩查询有分数吗,江苏省2021年普通高考录取分数线公布
- office 2017 (或2007)如何在方框里打钩 或 插入可打钩的方框
- python实现指纹识别毕业论文_(完整版)指纹识别系统本科毕业论文
- 使用Python做QQ机器人
- 百度地图实现测量面积和测量距离功能
- [经验] 轻松解读PID控制算法的三种参数的自整定方法
- 【Unity入门计划】基本概念(1)-2D刚体Rigidbody 2D
- 8255接口芯片与ADC0809的的使用
- 百度地图 行政区切换显示
- html,htm,html5,shtml的区别
- 九、逻辑回归介绍-恶性乳腺癌肿瘤预测