android 怎么通过android 蓝牙获取数据设备的数据

正在播放:
蓝牙输入设备读取数据(1.20)
通过以下安全检测,可以放心使用。
百度手机卫士
腾讯手机管家
腾讯手机管家
金山手机卫士
金山手机卫士
该程序使用认证机构颁发的数字证书签名,确保由开发者发布且发布后未经篡改。
&&程序名称:蓝牙输入设备读取数据
&&开发者:HexaD
&&认证机构:上海市数字证书认证中心有限公司
&&证书有效期:~
蓝牙输入设备读取数据屏幕截图(4张)
蓝牙输入设备读取数据 软件介绍
这个应用程序安装一个新的输入法(切换),可以从蓝牙输入设备读取数据。 这不是一个蓝牙键盘驱动程序,它仅支持上面列出的设备。模拟输入支持但要求应用程序使用BluezIME还支持它
蓝牙输入设备读取数据应用评论
打开安卓市场客户端的
更多页面中的扫描下载
扫描二维码自动下载该软件
小于1千热度
3.0及以上固件版本
支持屏幕:
适用分辨率
上架时间:
All rights reserved. 福建博瑞网络科技有限公司 版权所有  就快过年了,公司还不给发年终奖!我都快到大街上讨饭了,公司也不带管我的!
  蓝牙是啥我就不再说了,因为我肯定解释不清楚蓝牙是啥,有兴趣请移步至。
  在开始前我们需要准备好一部手机而不是模拟器,且手机已经打开调试模式并连接到电脑上。文中的本地设备均指我们自己的手机,而远程设备则是指其他的设备(电脑、其他手机或者其他)。
  通常情况下,我们对蓝牙的操作主要有:开启和关闭蓝牙、搜索周边设备、能被周边设备所发现、获取配对设备、蓝牙设备间的数据传输。
  1、打开蓝牙(当然首先要确保你的手机是有蓝牙设备的)
  蓝牙设备主要分为两部分,一部分为本地设备,另一部分为远程设备。
BluetoothAdapter&&本地设备,对蓝牙操作首先就需要有一个BluetoothAdapter实例。常用的几个方法如下:
  cancelDiscovery()&&取消本地蓝牙设备的搜索操作,如果本地设备正在进行搜索,那么调用该方法后将停止搜索操作。
  Disable()&&关闭蓝牙设备。
  Enable()&&打开蓝牙设备。相信大家都有过打开蓝牙的经历,一般情况下都会弹出一个窗口,说正在请求打开蓝牙设备,你是不是允许云云。
  getAddress()&&获取蓝牙设备的MAC地址。
  GetDefaultAdapter()&&获取本地的蓝牙设备
  getName()&&获取本地蓝牙的名称
  getRemoteDevice(String address)&&根据远程设备的MAC地址来获取远程设备
  startDiscovery()&&蓝牙设备开始搜索周边设备
BuletoothDevice&&远程设备。
    它所包含的方法和BluetoothAdapter一样,不再累述。
1 // 获取本地的蓝牙适配器实例 2
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 3
if(adapter!=null) 4
if(!adapter.isEnabled()) 6
//通过这个方法来请求打开我们的蓝牙设备 8
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 9
startActivity(intent);10
System.out.println("本地设备驱动异常!");15
  2、搜索周边设备
  对于Android查找发现蓝牙设备使用BluetoothAdapter类的startDiscovery()方法就可以执行一个异步方式获取周边的蓝牙设备,因为是一个异步的方法所以我们不需要考虑线程被阻塞问题,整个过程大约需要12秒时间,这时我们可以注册一个 BroadcastReceiver 对象来接收查找到的蓝牙设备信息,我们通过Filter来过滤ACTION_FOUND这个 Intent动作以获取每个远程设备的详细信息,通过Intent字段EXTRA_DEVICE 和 EXTRA_CLASS可以获得包含了每个BluetoothDevice 对象和对象的该设备类型 BluetoothClass。
  实现一个自己的BroadCastReceiver类,并注册这个类。
private class BluetoothReciever extends BroadcastReceiver { 2
@Override 4
public void onReceive(Context context, Intent intent) { 5
// TODO Auto-generated method stub 6
String action = intent.getAction(); 7
if (BluetoothDevice.ACTION_FOUND.equals(action)) { 8
BluetoothDevice device = intent 9
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);10
System.out.println(device.getAddress());11
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);2
bluetoothReceive = new BluetoothReciever();3
registerReceiver(bluetoothReceive, intentFilter);
  因为在注册一个Receiver后,程序并不知道该何时去回收它,所以需要我们自己重写Activity类的onDestroy()方法。
@Override2
protected void onDestroy() {3
// TODO Auto-generated method stub4
unregisterReceiver(bluetoothReceive);5
super.onDestroy();6
  3、被周边设备所发现  
  如果需要用户确认操作,不需要获取底层蓝牙服务实例,可以通过一个Intent来传递ACTION_REQUEST_DISCOVERABLE参数, 这里通过startActivity来请求开启。
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);2 //50这个参数代表的是蓝牙设备能在多少秒内被发现
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 50);3
startActivity(discoverableIntent);
  4、配对
  配对操作呢,一般都是发现设备后,由我们人工来进行选择后系统自动去配对。我们可以通过下面的方法来获得配对的设备:
//通过getBondedDevices方法来获取已经与本设备配对的设备 2
Set&BluetoothDevice& device= adapter.getBondedDevices(); 3
if(device.size()&0) 4
for(Iterator iterator=device.iterator();iterator.hasNext();) 6
BluetoothDevice bluetoothDevice=(BluetoothDevice)iterator.next(); 8
System.out.println(bluetoothDevice.getAddress()); 9
  5、数据交换  
  在看过前面的启动、发现/搜索、配对这些操作后,下面来说说数据传输的问题。
  在Android系统中,蓝牙设备间的数据传输问题和我们在PC上的网络编程颇为类似,有一端作为Server端监听Client端的连接请求,在二者建立了连接后,就可以使用普通的数据传输方式进行数据交换操作了。在这个过程中,我需要使用到BluetoothServerSocket和BluetoothSocket两个类来建立Server端和Client端,还需要使用到一些关于流(Stream)的知识。
BluetoothServerSocket&&服务端(监听端、监听器、接受请求的一端)
Accept()&&阻塞宿主线程,直至收到客户端请求。返回BluetoothSocket对象。由于这个
Accept(int timeout)&&阻塞宿主线程,直至收到客户端请求或等待时间超过timeout。返回BluetoothSocket对象。
Close()&&关闭BluetoothServerSocket监听器。
  可以看到,Accept方法是一个阻塞方法,所以在进行开发的时候,一般都需要用到多线程的知识。JAVA的多线程知识,可以在JAVA的JDK帮助文档中查看,就单纯的应用来说还是比较简单的。
BluetoothSocket&&客户端(请求端)
Close()&&关闭BluetoothSocket请求端。
Connect()&&主动向服务端(监听端)发起连接请求。
  在了解了这两个类后,可以着手来建立我们自己的Server端和Client端了。
  如果一个设备需要和两个或多个设备连接时,就需要作为一个server来传输,服务器端套接字在接受(accepted) 一个客户发来的BluetoothSocket连接请求时作出相应的响应。服务器socket将监听进入的连接请求,一旦连接被接受,将产生一个BluetoothSocket。
创建一个Server
  使用BluetoothAdapter类的listenUsingRfcommWithServiceRecord方法来新建一个ServerSocket。在listenUsingRfcommWithServiceRecord中有一个参数叫做UUID,UUID(Universally Unique Identifier)是一个128位的字符串ID,被用于唯一标识我们的蓝牙服务。你可以使用web上的任何一款UUID产生器为你的程序获取一个UUID,然后使用fromString(String)初始化一个UUID。
  使用ServerSocket实例的accept方法进行监听,当监听到带有我们初始化的UUID参数的连接请求后作出响应,连接成功后返回一个BluetoothSocket对象。连接完成后,调用close方法关闭该Socket监听。
// Bluetooth的ServerSocket包装类 2
class BluetoothServer { 3
public BluetoothServer() throws IOException { 4
// 要建立一个ServerSocket对象,需要使用adapter.listenUsingRfcommWithServiceRecord方法 7 // UUID可以在网上去申请 8
private BluetoothServerSocket serverSocket = adapter.listenUsingRfcommWithServiceRecord("myServerSocket", 9
UUID.fromString("84D1319C-FBAF-644C-901A-8F091F25AF04"));10
BluetoothSocket socket = serverSocket.accept();11 12
void m() throws IOException {13
if (socket != null) {14
InputStream inputStream = socket.getInputStream();15
int read = -1;16
final byte[] bytes = new byte[1024];17
for (; (read = inputStream.read(bytes)) & -1;) {18
final int count =19
Thread _start = new Thread(new Runnable() {20 21
@Override22
public void run() { 23
// TODO Auto-generated method stub24
StringBuilder sb = new StringBuilder();25
for (int i = 0; i & i++) {26
if (i & 0) {27
sb.append(' ');28
String _s = Integer.toHexString(bytes[i] & 0xFF);30
if (_s.length() & 2) {31
sb.append('0');32
sb.append(_s);34
System.out.println(sb.toString());36
_start.start();39
创建一个Client
  创建一个Client端,首先需要我们使用BluetoothDevice的实例的createRfcommSocketToServiceRecord方法来创建一个BluetoothSocket实例。在创建的时候,需要给createRfcommSocketToServiceRecord方法传入我们服务端的UUID值。然后使用BluetoothSocket实例的Connect方法对Server端进行连接请求,当连接成功后,Client端和Server端的传输通道就被打开。最后在连接完成后使用该实例的close方法来关闭这个连接。
1 class BluetoothClient { 2
BluetoothDevice device = null; 3
//通过构造函数来传入一个BluetoothDevice实例 5
public BluetoothClient(BluetoothDevice device) { 6
this.device = 7
BluetoothSocket socket = null; 9
void connetServer() throws IOException {10
Thread _clientThread = new Thread(new Runnable() {11
public void run() {12
//通过BluetoothDevice实例的createRfcommSocketToServiceRecord方法可以返回一个带有UUID的BluetoothSocket实例14
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("84D1319C-FBAF-644C-901A-8F091F25AF04"));15
} catch (IOException e1) { 16
// TODO Auto-generated catch block17
e1.printStackTrace();18
socket.connect();21
} catch (IOException e1) { 22
// TODO Auto-generated catch block23
e1.printStackTrace();24
if (socket != null) {26
socket.close();28
} catch (Exception e) {29
// TODO: handle exception30
_clientThread.start();35
  getInputStream()&&获得一个可读的流,该流在连接不成功的情况下依旧可以获得,但是对其操作的话就会报IOException的异常。需要从外部获取的数据都从该流中获取。
  getOutputStrem()&&获得一个可写的流,该流在连接不成功的情况下依旧可以获得,但是对其操作的话就会报IOException的异常。需要往外部传输的数据都可以写到该流中传输出去。
  数据传输的大致流程如下:
首先,分别通过getInputStream()和getOutputStream()获得管理数据传输的InputStream和OutputStream。
然后,开辟一个线程专门用于数据的读或写。这是非常重要的,因为read(byte[])和write(byte[])方法都是阻塞调用。read(byte[])从输入流(InputStream)中读取数据。write(byte[])将数据写入到OutputStream流中去,这个方法一般不会阻塞,但当远程设备的中间缓冲区已满而对方没有及时地调用read(byte[])时将会一直阻塞。所以,新开辟的线程中的主循环将一直用于从InputStream中读取数据。
  还要补充一点,由于蓝牙设备是系统设备,所以需要有相应的权限支持。在AndroidManifest.xml文件中添加上权限。
&uses-permission android:name="android.permission.BLUETOOTH"&&/uses-permission&2
&uses-permission android:name="android.permission.BLUETOOTH_ADMIN"&&/uses-permission&
  在这个笔记中将了下蓝牙的一些简单的操作,包括蓝牙的开启、搜索、配对等操作,同时还有数据的传输问题,关于数据的传输的相关操作最重要的其实是多线程的操作和数据流的操作,所以对这两点不熟悉的同学可以去看看java的JDK帮助文档。在下节中,将会将一些WIFI的操作,因为WIFI内容不是很多,所以还将附加上一些Android网络编程方面的内容。
阅读(...) 评论()在很多方面,蓝牙是一种能够发送或接受两个不同的设备之间传输的数据。 Android平台包含了蓝牙框架,使设备以无线方式与其他蓝牙设备进行数据交换的支持。
Android提供蓝牙API来执行这些不同的操作。
扫描其他蓝牙设备
获取配对设备列表
连接到通过服务发现其他设备
Android提供BluetoothAdapter类蓝牙通信。通过调用创建的对象的静态方法getDefaultAdapter()。其语法如下给出。
private BluetoothAdapter BA;
BA = BluetoothAdapter.getDefaultAdapter();
为了使用设备的蓝牙,调用下列蓝牙ACTION_REQUEST_ENABLE的意图。其语法如下:
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
除了这个常量,有提供其它的API,支持不同任务的其他常数。它们在下面列出。
ACTION_REQUEST_DISCOVERABLE
此常数用于开启蓝牙的发现
ACTION_STATE_CHANGED
此常量将通知蓝牙状态已经改变
ACTION_FOUND
此常数用于接收关于所发现的每个设备的信息
启用了蓝牙功能之后,可以通过调用 getBondedDevices()方法来获取配对设备列表。它返回一组的蓝牙设备。其语法如下:
private Set&BluetoothDevice&pairedDevices;
pairedDevices = BA.getBondedDevices();
除了配对的设备,还有API,让更多蓝牙控制权等方法。它们在下面列出。
方法及说明
这种方法使适配器,如果未启用
isEnabled()
如果适配器已启用此方法返回true
该方法禁用适配器
此方法返回的蓝牙适配器的名称
setName(String name)
此方法更改蓝牙名称
getState()
此方法返回蓝牙适配器的当前状态
startDiscovery()
此方法开始蓝牙120秒的发现过程。
这个例子提供了示范BluetoothAdapter类操纵蓝牙,并显示通过蓝牙配对设备列表。
为了试验这个例子,需要在实际设备上运行此程序
使用Android Studio创建Android应用程序,并将其命名为Bluetooth,创建这个项目,确保目标SDK编译在Android SDK的最新版本或使用更高级别的API。
修改&src/MainActivity.java&文件中添加代码
如果修改所需的布局XML文件&res/layout/activity_main.xml &添加GUI组件
修改&res/values/string.xml &文件,并添加必要的字符串常量组件
修改 AndroidManifest.xml&添加必要的权限。
运行应用程序并选择运行Android的设备,并在其上安装的应用和验证结果。
以下是&src/com.yiibai.bluetooth/MainActivity.java 文件的内容:
package com.example.bluetooth;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button On,Off,Visible,list;
private BluetoothAdapter BA;
private Set&BluetoothDevice&pairedDevices;
private ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
On = (Button)findViewById(R.id.button1);
Off = (Button)findViewById(R.id.button2);
Visible = (Button)findViewById(R.id.button3);
list = (Button)findViewById(R.id.button4);
lv = (ListView)findViewById(R.id.listView1);
BA = BluetoothAdapter.getDefaultAdapter();
public void on(View view){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(),&Turned on&
,Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(),&Already on&,
Toast.LENGTH_LONG).show();
public void list(View view){
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices)
list.add(bt.getName());
Toast.makeText(getApplicationContext(),&Showing Paired Devices&,
Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter
(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
public void off(View view){
BA.disable();
Toast.makeText(getApplicationContext(),&Turned off& ,
Toast.LENGTH_LONG).show();
public void visible(View view){
Intent getVisible = new Intent(BluetoothAdapter.
ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
public boolean onCreateOptionsMenu(Menu menu) {
// I this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
这里是&activity_main.xml&文件的内容:
xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:paddingBottom=&@dimen/activity_vertical_margin&
android:paddingLeft=&@dimen/activity_horizontal_margin&
android:paddingRight=&@dimen/activity_horizontal_margin&
android:paddingTop=&@dimen/activity_vertical_margin&
tools:context=&.MainActivity&
android:id=&@+id/scrollView1&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_alignParentBottom=&true&
android:layout_alignParentLeft=&true&
android:layout_alignParentRight=&true&
android:layout_alignParentTop=&true&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical&
android:id=&@+id/textView1&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:text=&@string/app_name&
android:textAppearance=&?android:attr/textAppearanceLarge&
android:id=&@+id/button1&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:onClick=&on&
android:text=&@string/on&
android:id=&@+id/button2&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:onClick=&visible&
android:text=&@string/Visible&
android:id=&@+id/button3&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:onClick=&list&
android:text=&@string/List&
android:id=&@+id/button4&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:onClick=&off&
android:text=&@string/off&
android:id=&@+id/listView1&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:visibility=&visible&
这里是&Strings.xml&文件的内容:
&?xml version=&1.0& encoding=&utf-8&?&
name=&app_name&Bluetooth
name=&action_settings&Settings
name=&hello_world&Hello world!
name=&on&Turn On
name=&off&Turn Off
name=&Visible&Get Visible
name=&List&List Devices
这里是&AndroidManifest.xml&文件的内容:
&?xml version=&1.0& encoding=&utf-8&?&
xmlns:android=&/apk/res/android&
package=&com.yiibai.bluetooth&
android:versionCode=&1&
android:versionName=&1.0&
android:minSdkVersion=&8&
android:targetSdkVersion=&17&
android:name=&android.permission.BLUETOOTH&
android:name=&android.permission.BLUETOOTH_ADMIN&
android:allowBackup=&true&
android:icon=&@drawable/ic_launcher&
android:label=&@string/app_name&
android:theme=&@style/AppTheme&
android:name=&com.yiibai.bluetooth.MainActivity&
android:label=&@string/app_name&
android:name=&android.intent.action.MAIN&
android:name=&android.intent.category.LAUNCHER&
让我们试着运行AndroidCapture应用程序。假设你已经连接实际的Android移动设备到计算机。启动应用程序之前,Eclipse会显示如下窗口,选择要运行的Android应用程序的选项。
选择移动设备作为一个选项,然后检查移动设备将显示如下界面:
现在选择打开开启蓝牙。但是当选择它,蓝牙将不会被打开。事实上它会询问许可,以启用蓝牙。
现在,只需要选择设置可见按钮来打开视图。下面的屏幕会出现要求许可才能打开发现120秒。
现在,只要选择列表中的设备选项。它会列出倒在列表视图中的配对设备。就我而言,只有一个配对设备。它如下所示。
现在,只需选择关闭按钮来关闭蓝牙。当关掉蓝牙指示成功切换关闭蓝牙会出现以下消息。
以下代码下载:
本站代码下载:如何使用android API函数获取CC2540的电池电量? - 蓝牙Bluetooth 技术 - 德州仪器在线技术支持社区
如何使用android API函数获取CC2540的电池电量?
发表于3年前
<input type="hidden" id="hGroupID" value="42"
我用函数获取了服务,特性后,想从特性里读出特性值,发现读不出。。。&/h4>&div style=&clear:&>&/div>" />
如何使用android API函数获取CC2540的电池电量?
此问题尚无答案
All Replies
我用函数获取了服务,特性后,想从特性里读出特性值,发现读不出。。。
You have posted to a forum that requires a moderator to approve posts before they are publicly available.
榜眼39505分
Hi weixin,
你想读的特征值的属性是什么?可读的吗?
You have posted to a forum that requires a moderator to approve posts before they are publicly available.

我要回帖

更多关于 android 蓝牙获取数据 的文章

 

随机推荐