代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
android蓝牙基础
作者:
wangchenfeng
/ 发布于
2015/2/15
/
634
package com.blueteeth; import java.util.ArrayList; import android.os.Bundle; 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.util.Log; import android.view.Menu; public class BluetoothActivity extends Activity { private final static String TAG = "BluetoothActivity"; BluetoothAdapter bluetoothAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_blueteeth); initBluetooth(); enableDiscovery(); } /** * 初始化,获取蓝牙基础信息 */ private void initBluetooth(){ bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter.isEnabled()) { String address = bluetoothAdapter.getAddress(); String name = bluetoothAdapter.getName(); Log.d(TAG,address+":"+name); }else { //启动蓝牙 startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 0); } } private void startDiscovery(){ if (bluetoothAdapter.isEnabled()) { bluetoothAdapter.startDiscovery(); } } /** * 蓝牙发现设备,发现结束广播 */ private BroadcastReceiver discoveryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED; String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED; if (dStarted.equals(intent.getAction())) { //启动发现过程 Log.d(TAG, "Discovery started"); }else if(dFinished.equals(intent.getAction())){ //发现过程完成 Log.d(TAG,"Discovery compeleted"); } } }; private void registerDiscovery(){ IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);; intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);; registerReceiver(discoveryReceiver, intentFilter); } //发现蓝牙设备 ArrayList<BluetoothDevice> devices = new ArrayList<BluetoothDevice>(); private void discoveryDevices(){ registerReceiver(discoveryResult, new IntentFilter(BluetoothDevice.ACTION_FOUND)); if (bluetoothAdapter.isEnabled()&&!bluetoothAdapter.isDiscovering()) { devices.clear(); bluetoothAdapter.startDiscovery(); } } BroadcastReceiver discoveryResult = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String remoteDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); devices.add(remoteDevice); Log.d(TAG, "Discovered "+remoteDeviceName); } }; private void register(){ registerReceiver(bluetoothReceiver,new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); } //蓝牙状态改变 BroadcastReceiver bluetoothReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { int state = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_STATE, -1); int previousState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1); String tt = ""; switch (state) { case BluetoothAdapter.STATE_TURNING_ON: tt = "Bluetooth turning on"; break; case BluetoothAdapter.STATE_ON: tt = "Bluetooth on"; break; case BluetoothAdapter.STATE_OFF: tt = "Bluetooth off"; break; case BluetoothAdapter.STATE_TURNING_OFF: tt = "Bluetooth turning off"; break; default: break; } Log.d(TAG, tt); } }; BroadcastReceiver modeChangedReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { String previousScanMode = BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE; String scanMode = BluetoothAdapter.EXTRA_SCAN_MODE; int currentmode = intent.getIntExtra(previousScanMode, -1); int prevMode = intent.getIntExtra(scanMode, -1); new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); } }; //设备的可发现性 private void canDiscovery(){ int discoveryState = bluetoothAdapter.getScanMode(); switch (discoveryState) { case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE: break; case BluetoothAdapter.SCAN_MODE_CONNECTABLE: break; case BluetoothAdapter.SCAN_MODE_NONE: break; default: break; } } //设置蓝牙可以被发现 private void enableDiscovery(){ startActivityForResult(new Intent(bluetoothAdapter.ACTION_REQUEST_DISCOVERABLE), 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case 1: if (resultCode==RESULT_CANCELED) { Log.d(TAG, "Bluetooth 不能被发现"); } break; default: break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.blueteeth, menu); return true; } }
试试其它关键字
蓝牙基础
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
wangchenfeng
贡献的其它代码
(
8
)
.
Java RC4 加密解密
.
读取手机相册图片并显示
.
计算德州扑克牌面值
.
剪刀石头布
.
检测页面关闭
.
二维数组去重
.
android电话应用基础知识点
.
android蓝牙基础
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3