- 浏览: 21123 次
最新评论
1、修改本蓝牙设备的可见性
2、扫描周围可用蓝牙设备
步骤:
1、在androidManifest.xml中添加以下代码
2、在布局文件main.xml中添加两个按钮
3、Activity文件
2、扫描周围可用蓝牙设备
步骤:
1、在androidManifest.xml中添加以下代码
<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
2、在布局文件main.xml中添加两个按钮
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/discoverId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="设置可见性" /> <Button android:id="@+id/scanId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="开始扫描" /> </LinearLayout>
3、Activity文件
import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestBluetooth02Activity extends Activity { /** Called when the activity is first created. */ private Button discoverButton = null; private Button scanButton = null; private BluetoothReceiver bluetoothReceiver = null; private BluetoothAdapter bluetoothAdapter = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); discoverButton = (Button)findViewById(R.id.discoverId); discoverButton.setOnClickListener(new DiscoverButtonListener()); //创建一个IntentFilter对象,将其action制定为BluetoothDevice.ACTION_FOUND IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); //生成一个BluetoothReceive对象 bluetoothReceiver = new BluetoothReceiver(); //注册广播接收器 registerReceiver(bluetoothReceiver,intentFilter); bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); scanButton = (Button)findViewById(R.id.scanId); scanButton.setOnClickListener(new ScanButtonListener()); } private class DiscoverButtonListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub //创建一个Intent对象,并惊奇Action的值设置为BluetoothAdapter.ACTION_REQEST_DISCOVERABLE Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); //将一个键值对放在Intent对象中,主要用于指定可见状态的持续时间 discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 500); startActivity(discoverIntent); } } private class BluetoothReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { //可以从收到的Intent对象当中,将代表远程的蓝牙适配器的对象取出 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); System.out.println(device.getAddress()); } } } private class ScanButtonListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub bluetoothAdapter.startDiscovery(); } } /* protected void onDestroy { unregisterReceiver(bluetoothReceiver); super.onDestroy(); }*/ }
发表评论
-
SQLite数据库
2011-10-21 11:12 9961。从www.sqlite.org下载SQLite 3.3.4 ... -
android中的有道词典实例
2011-10-19 22:51 8991、布局文件main.xml <?xml version ... -
mars老师的googleMap示例(二)
2011-10-15 17:14 849manifest.xml文件 <?xml version ... -
mars老师的googleMap(一)
2011-10-15 10:43 795一、申请 Apikey Apikey Apikey Apike ... -
Intent在android中的几种用法
2011-10-14 09:26 757如果是从BroadcastReceiver 启动一个新的Act ... -
基于Service与ContentProvider的音乐播放实例
2011-10-13 23:37 731Android的核心也就是Activi ... -
android之用户定位(一)
2011-10-13 19:53 16711、User Location 能做什么 1) 获取用户的位置 ... -
android之蓝牙操作(一)
2011-10-13 16:35 1208与蓝牙相关的API 1、BluetoothAdapter ... -
Android学习之JSON数据解析
2011-10-12 13:30 681在Android应用开发中,常用的数据交换格式有XML和JSO ... -
android中的JSON解析
2011-10-12 10:17 8891 。 什么是JSON 就是现在网络上比较流行 ... -
Animations的使用(六)
2011-10-11 17:44 1187LayoutAnimationController的使用方法( ... -
Animations的使用(五)
2011-10-11 17:41 7051 AnimationSet的使用方法 什么是Animat ... -
Animations使用 (四)
2011-10-11 17:36 551Animations的第二种使用方法(第一种见1) 步骤: ... -
android面试
2011-10-10 21:12 766为什么要用ContentProvider?它和sql的实现上有 ... -
android中的animations的用法(三)
2011-10-10 21:07 694一 LayoutAnimationController ... -
android中的animations的用法(二)
2011-10-10 20:59 771一 AnimationSet 的用法 二 Interpol ... -
android中的animations的用法 (一)
2011-10-10 20:56 1309Animations 可分为两大类: 一 Tweened A ...
相关推荐
采用kotlin 对蓝牙进行打开和关闭,以及搜索蓝牙设备,与其他蓝牙设备配对,并将所有配对的信息进行显示。相关说明看 https://mp.csdn.net/mp_blog/creation/editor/119547298
android平台的蓝牙读卡器操作,首先通过蓝牙连接读卡器,连接成功后,发送读卡命令,读取附近的射频卡,读卡器返回卡号后,停止读卡。
Android 蓝牙BLE全面解析以及智能车锁开发实例一、蓝牙BLE产生背景——蓝牙的发展历程 二、蓝牙BLE的基本概念 三、蓝牙BLE的架构介绍 1. 蓝牙BLE架构概览 2. 简述BLE如何发送数据包 2.1 广播方式 2.2 连接方式 四、...
Android 蓝牙操作,可以用于与下位机硬件部分通信牙模块。 Android 蓝牙操作。Android 蓝牙操作。
android 简单抽取的ble蓝牙库,用于操作ble的
Android蓝牙开发客户端代码,必须配合服务器端进行操作,
android 蓝牙操作的demo
android 好用的ble蓝牙调试助手,方便操作,查看服务的UUID
Android蓝牙相关操作源代码,包括蓝牙扫描、连接、通讯相关一操作.
用于连接小车上的蓝牙模块,实现对小车的遥控。这个APP是通过JAVA代码...小车蓝牙和手机端成功完成配对,使用手机APP上的键盘控制给小车发送指令,小车上的芯片收到了指令后进行处理,最后小车能够完成对应指令的操作。
Android Stdio开发,可以搜索蓝牙,连接蓝牙等操作
Android-BLE,AndroidBLE 蓝牙框架,包括扫描、连接、设置通知、发送数据、读取、接收数据和 OTA 升级以及各种直观的回调,近乎一行代码植入项目,可扩展配置蓝牙相关操作。.zip
操作蓝牙的代码demo.通过demo可以基本了解蓝牙通讯的流程。
Android手机蓝牙控制,讲用android手机控制蓝牙,对蓝牙的操作和实用,简介明了,是个很好的文档
android蓝牙工具类,包括连接蓝牙,发送数据等操作
Android BLE 蓝牙开发框架,使用回调方式处理,搜索、连接、notify、indicate、读、写等一系列蓝牙操作
基于Android平台的一个关于应用蓝牙设备进行聊天的程序,目前程序已实现2台设备之间的通信,服务器部分是按照的一对多的方式写的```没有测试3台设备是否能聊天,不过2台设备配对后``是百分百可以的,有UI界面,操作简单,...
android操作蓝牙4.0的代码,手机端需要三星4.1.2以上系统,经过测试,三星的S3和note2都可用,蓝牙模块用的是Ti的CC2540模块,本资源包含了手机端的代码和模块的代码,欢迎广大有需要的朋友下载
Android 是Google 于2007 年11 月5 日宣布的基于Linux 平台开源手机操作系统名称,该平 台由操作系统、中间件、用户界面和应用软件组成,号称是首个为移动终端打造的真正开放 和完整的移动软件。本文通过研究蓝牙...
blesdk 该项目基于安卓蓝牙ble协议实现的一个sdk、可以实现扫描、连接、发送指令等功能