一.修改本机蓝牙设备的可见性
二.扫描周围可用的蓝牙设备
Eg:
一. 清单文件AdroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.se7en"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<!-若需要管理蓝牙设备,如修改可见性,则需以下的权限->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
</manifest>
二. 布局文件: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/discoverButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="设置可见性"/>
<Button
android:id="@+id/scanButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始扫描"/>
</LinearLayout>
三. MainActivity:
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 MainActivity extends Activity {
private Button discoverButton = null;
private Button scanButton = null;
private BluetoothAdapter adapter = null;
private BluetoothReceiver bluetoothReceiver = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = BluetoothAdapter.getDefaultAdapter();
discoverButton = (Button)findViewById(R.id.discoverButton);
scanButton = (Button)findViewById(R.id.scanButton);
//修改蓝牙设备的可见性
discoverButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
Intent discoverIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//设置蓝牙可见性,500表示可见时间(单位:秒),当值大于300时默认为300
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,500);
startActivity(discoverIntent);
}
});
scanButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//开始扫描周围蓝牙设备,该方法是异步调用并以广播的机制返回,所以需要创建一个BroadcastReceiver来获取信息
adapter.startDiscovery();
}
});
//设定广播接收的filter
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//创建蓝牙广播信息的receiver
bluetoothReceiver = new BluetoothReceiver ();
//注册广播接收器
registerReceiver(bluetoothReceiver,intentFilter);
}
private class BluetoothReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//获得扫描到的远程蓝牙设备
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
System.out.println(device.getAddress());
}
}
}
分享到:
相关推荐
Android蓝牙开发框架! !
基于Android的BlueTooth开发手机蓝牙和蓝牙模块通讯,
可直接在HBuilderX 上运行,亲测可用。实现蓝牙状态实时监控,数据的接收和发送。外加Android原生蓝牙实现源码 + 串口调试工具
二 Android Bluetooth架构 2.1 Bluetooth架构图 2.2 Bluetooth 代码层次结构 三 Bluetooth 协议栈分析 3.1 蓝牙协议栈 3.2 Android 与蓝牙协议栈的Bluetooth 之HCI层分析 .4.1HCI层与基带的通信方 4.2包的分析及研究...
实现了Android三种蓝牙扫描方法。分别是注册蓝牙扫描广播,LeScanCallback和ScanCallback回调。
通过Android开发实现手机间通过蓝牙传输文件 实验小作业,详细分析 安卓平台提供对蓝牙的通讯栈的支持,允许设备和其他的设备进行无线传输数据。应用程序层通过安卓API来调用蓝牙的相关功能,这些API使程序无线连接...
delphi-android-bluetooth-printer-sample-master
Android-5.0-Bluetooth-HCI-Reqs
蓝牙协议栈bluetooth-src 蓝牙协议栈bluetooth-src
Unity资源商店7.99刀的插件,用于Unity开发安卓的经典蓝牙传输 Unity商店地址:https://assetstore.unity.com/packages/tools/network/simple-bluetooth-for-android-140551
Android 蓝牙相关小Demo,实现的功能是点击按钮自动打开蓝牙并打印周边蓝牙设备。 (针对android 6.0以下) 文件上部是 MainActivity 的,下部是对应的布局文件。直接粘过去运行就可以了。 权限加一下: <uses-...
该用例是AndroidBLE蓝牙连接小设备的搜索,连接以及读取的相关用例。 如果还不对BLEandroid蓝牙的认识以及API有相关的认识:,那么请访问我的博客: http://blog.csdn.net/androidstarjack/article/details/60468468...
Android 蓝牙低功耗ble 广播、扫描、连接、数据读写源码流程图,非常详细的从btif-bta-btm-hci 数据流程走向,以及从controller收到数据到btm层,将Android 源码使用流程图的形式画了出来,使Android 蓝牙开发者更...
Android-Simple-Bluetooth-Example Simple Android Bluetooth example to turn on/off radio and to view and connect with devices. Has associated code to connect to an Arduino. For a complete tutorial write...
此应用程序支持不同的蓝牙设备: Bluetooth Classic(实施标准蓝牙SPP配置文件): – HC-05,HC-06,…… – Raspberry Pi 3 – …… 蓝牙LE(实施供应商特定的蓝牙GATT服务): – 预定义: * Nordic Semic
支持RCSP协议的固件,支持以下系列芯片 | AC693X, AC697X, AC695X等等 |最后列新... |- 杰理OTA外接库(Android)开发文档 --- 在线开发文档 |- 杰理之家SDK(Android)开发文档 --- 在线开发文档 |- libs -- 核心库<br
Android BLE 蓝牙开发框架,使用回调方式处理,搜索、连接、notify、indicate、读、写等一系列蓝牙操作
最新版Unity蓝牙插件 Bluetooth LE for iOS tvOS and Android 2.55
Android手机可以使用Bluetooth Low Engery或Bluetooth Classic同时与多个蓝牙设备进行通信。 蓝牙经典此代码旨在使用三种服务与三种蓝牙经典设备进行通信。 MyService1.java和MyService2.java与三个蓝牙设备有关。 ...