文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > Android平台蓝牙编程
侵权投诉

Android平台蓝牙编程

Package name: android.bluetooth.*,主要相关类介绍如下:

* BluetoothAdapter: 本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成;
* BluetoothDevice: 蓝牙设备类,代表了蓝牙通讯过程中的远端设备;
* BluetoothSocket: 蓝牙通讯套接字,代表了与远端设备的连接点,使用socket本地程序可以通过inputstream和outputstream与远端程序进行通讯;
* BluetoothServerSocket: 服务器通讯套接字,与TCP ServerSocket类似;
* BluetoothClass: 用于描述远端设备的类型,特点等信息,通过getBluetoothClass()方法获取代表远端设备属性的BluetoothClass对象。

使用蓝牙必须获取的权限:

<uses-permission android:name="android.permission.BLUETOOTH" />

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

建立蓝牙连接:

通过BluetoothAdapter.getDefaultAdapter()方法获取BluetoothAdapter对象。

判断当前蓝牙是否启动,如果没有启动提示用户手动启动:

if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

在Activity的onActivityResult()方法中,对用户的设定结果进行处理。

搜寻远端蓝牙设备

首先获取已配对的远端设备:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

然后通过BluetoothAdapter.startDiscovery()方法启动蓝牙设备的搜寻。这是个异步方法,调用的时候立刻就会返回。为了获得搜寻的结果,必须在用户自己的Activity中注册一个BroadcastReceiver,代码如下:

// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy

设置本地设备可以被发现

只有将本地设备设置为可被发现,远端的蓝牙设备才能够找到并和本地设备建立连接。通过下面的代码发送Intent对象,让用户手动启动可发现设置。

Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERAB

第1页

你可能喜欢

热门文档

相关文档

  • ANDROID蓝牙编程

    ANDROID 蓝牙编程用 BluetoothAdapter 类,你能够在 Android 设备上查找周边的蓝牙...在 Android 平台中首先我们需要查找本地活动的蓝牙适配器,通过 BluetoothAdapter ...

  • Android蓝牙编程

    ANDROID 蓝牙编程用 BluetoothAdapter 类,你能够在 Android 设备上查找周边的蓝牙...在 Android 平台中首先我们需要查找本地活动的蓝牙适配器,通过 BluetoothAdapter ...

  • android蓝牙开发步骤

    百度文库_文档分享平台 新闻网页贴吧知道音乐图片视频地图百科文库 ... Android蓝牙编程 27页 免费 android_蓝牙开发指南 10页 1下载券 Android蓝牙...

  • android_蓝牙开发指南

    的聊天系统一样用一个蓝牙手机当服务器,一个当客户端,在用一个类当做 连接的管理类就行了 android 平台蓝牙编程 Android 平台支持蓝牙网络协议栈,实现蓝牙设备...

  • 蓝牙编程之蓝牙聊天分析(一)

    Ophone 平台蓝牙编程之蓝牙聊天分析(一) 平台蓝牙编程之蓝牙聊天分析( OPhone ...android:layout_height="match_parent" android:layout_weight="1" /> <Text...

  • BLE 和经典蓝牙Android编程说明

    百度文库_文档分享平台 新闻网页贴吧知道音乐图片视频地图百科文库 ... BLE 和经典蓝牙Android编程说明_计算机软件及应用_IT/计算机_专业资料。BLE 和...

  • Android蓝牙系统分析与开发

    前言: 随着嵌入式系统的飞速发展,很多嵌入式平台上需要 Bluetooth 设备和应 用... Android蓝牙编程 27页 免费 蓝牙驱动及Bluez使用流程... 46页 免费 Android...

  • Android蓝牙开发(整理)

    Android平台提供的蓝牙API去实现蓝牙设备之间的通信, 蓝牙设备之间的通信主要包括 ...指定连接的那个远程蓝牙设备 下面说说具体的编程实现 1.启动蓝牙功能: 首先通过...

  • Android蓝牙开发小结(转)

    Android平台提供的蓝牙API去实现蓝牙设备之间的通信, 蓝牙设备之间的通信主要包括 ...下面说说具体的编程实现 1.启动蓝牙功能: 首先通过调用静态方法getDefaultAdapter(...

  • android 蓝牙

    的聊天系统一样用一个蓝牙手机当服务器,一个当客户端,在用一个类当做 连接的管理类就行了 android 平台蓝牙编程 Android 平台支持蓝牙网络协议栈,实现蓝牙设备...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email