当前位置:首页 » 安卓系统 » android判断蓝牙连接

android判断蓝牙连接

发布时间: 2022-05-24 08:12:35

A. Android怎么检测蓝牙的连接状态如果一段断开,我这边怎么检测得到

按照蓝牙规范,一旦超时连接断开,会返回上层Disconnect complete Event with reason code: supervision timeout.可以根据该原因码,检测连接状态,具体方法为:
1、BluetoothAdapter 顾名思义,蓝牙适配器,直到建立bluetoothSocket连接之前,都要不断操作它BluetoothAdapter里的方法很多,常用的有以下几个:cancelDiscovery() 根据字面意思,是取消发现,也就是说当正在搜索设备的时候调用这个方法将不再继续搜索disable()关闭蓝牙enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:Intemtenabler=newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler,reCode);//同startActivity(enabler);

getAddress()获取本地蓝牙地址getDefaultAdapter()获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdaptergetName()获取本地蓝牙名称。
2、getRemoteDevice(String address)根据蓝牙地址获取远程蓝牙设备getState()获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)isDiscovering()判断当前是否正在查找设备,是返回true***isEnabled()判断蓝牙是否打开,已打开返回true,否则,返回false***(String name,UUID uuid)根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步startDiscovery()开始搜索,这是搜索的第一步2.BluetoothDevice看名字就知道,这个类描述了一个蓝牙设备(UUIDuuid)根据UUID创建并返回一个BluetoothSocket这个方法也是我们获取BluetoothDevice的目的——创建BluetoothSocket

这个类其他的方法,如getAddress(),getName(),同BluetoothAdapter。

B. 哪位清楚android判断蓝牙是否连接

设置——打开蓝牙,看你连接了谁,下面会有已连接

C. android 怎么判断连接了那个蓝牙耳机

ba = BluetoothAdapter.getDefaultAdapter();// int isBlueCon;//蓝牙适配器是否存在,即是否发生了错误 if (ba == null){// isBlueCon = -1; //error return -1; } else if(ba.isEnabled()) { int a2dp = ba.getProfileConnectionState(BluetoothProfile.A2DP); //可操控蓝牙设备,如带播放暂停功能的蓝牙耳机 int headset = ba.getProfileConnectionState(BluetoothProfile.HEADSET); //蓝牙头戴式耳机,支持语音输入输出 int health = ba.getProfileConnectionState(BluetoothProfile.HEALTH); //蓝牙穿戴式设备

D. android 怎么判断蓝牙连接状态

在设置——蓝牙里,在显示的蓝牙列表里有已连接或者断开状态

E. android 怎么判断蓝牙配对成功

可以通过mDevice.getBondState()进行判断是否需要配对;

如下代码中: (mBluetoothDevice.getBondState()==BluetoothDevice.BOND_NONE表示未配对。可以在调用配对方法之后读取一下这个状态来判断是否已配对成功。)


protectedvoidconnectDevice(){

try{

//连接建立之前的先配对

if(mBluetoothDevice.getBondState()==BluetoothDevice.BOND_NONE){

MethodcreMethod=BluetoothDevice.class

.getMethod("createBond");

Log.e("TAG","开始配对");

creMethod.invoke(mBluetoothDevice);

}else{

}

}catch(Exceptione){

//TODO:handleexception

//DisplayMessage("无法配对!");

e.printStackTrace();

}

mBluetoothAdapter.cancelDiscovery();

try{

socket.connect();

//DisplayMessage("连接成功!");

//connetTime++;

connected=true;

}catch(IOExceptione){

//TODO:handleexception

//DisplayMessage("连接失败!");

connetTime++;

connected=false;

try{

socket.close();

socket=null;

}catch(IOExceptione2){

//TODO:handleexception

Log.e(TAG,"");

}

}finally{

connecting=false;

}

}


F. 如何实现android蓝牙开发 自动配对连接,并不弹出提示框

我就开始查找怎么关闭这个蓝牙配对提示框,后面还是伟大的android源码帮助了我。
在源码 BluetoothDevice 类中还有两个隐藏方法
cancelBondProcess()和cancelPairingUserInput()
这两个方法一个是取消配对进程一个是取消用户输入
下面是自动配对的代码
Mainfest,xml注册

<receiverandroid:name=".">

<intent-filter>

<actionandroid:name="android.bluetooth.device.action.PAIRING_REQUEST"/>

</intent-filter>

</receiver>

自己在收到广播时处理并将预先输入的密码设置进去

java">
{

StringstrPsw="0";

@Override
publicvoidonReceive(Contextcontext,Intentintent)
{
//TODOAuto-generatedmethodstub
if(intent.getAction().equals(
"android.bluetooth.device.action.PAIRING_REQUEST"))
{
BluetoothDevicebtDevice=intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

//byte[]pinBytes=BluetoothDevice.convertPinToBytes("1234");
//device.setPin(pinBytes);
Log.i("tag11111","ddd");
try
{
ClsUtils.setPin(btDevice.getClass(),btDevice,strPsw);//手机和蓝牙采集器配对
ClsUtils.createBond(btDevice.getClass(),btDevice);
ClsUtils.cancelPairingUserInput(btDevice.getClass(),btDevice);
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}


}
}
<b>/************************************蓝牙配对函数***************/
importjava.lang.reflect.Field;
importjava.lang.reflect.Method;

importandroid.bluetooth.BluetoothDevice;
importandroid.util.Log;
publicclassClsUtils
{

/**
*与设备配对参考源码:platform/packages/apps/Settings.git
*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
staticpublicbooleancreateBond(ClassbtClass,BluetoothDevicebtDevice)
throwsException
{
MethodcreateBondMethod=btClass.getMethod("createBond");
BooleanreturnValue=(Boolean)createBondMethod.invoke(btDevice);
returnreturnValue.booleanValue();
}

/**
*与设备解除配对参考源码:platform/packages/apps/Settings.git
*/Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
*/
staticpublicbooleanremoveBond(ClassbtClass,BluetoothDevicebtDevice)
throwsException
{
MethodremoveBondMethod=btClass.getMethod("removeBond");
BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice);
returnreturnValue.booleanValue();
}

staticpublicbooleansetPin(ClassbtClass,BluetoothDevicebtDevice,
Stringstr)throwsException
{
try
{
MethodremoveBondMethod=btClass.getDeclaredMethod("setPin",
newClass[]
{byte[].class});
BooleanreturnValue=(Boolean)removeBondMethod.invoke(btDevice,
newObject[]
{str.getBytes()});
Log.e("returnValue",""+returnValue);
}
catch(SecurityExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(IllegalArgumentExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returntrue;

}

//取消用户输入
(ClassbtClass,
BluetoothDevicedevice)

throwsException
{
MethodcreateBondMethod=btClass.getMethod("cancelPairingUserInput");
//cancelBondProcess()
BooleanreturnValue=(Boolean)createBondMethod.invoke(device);
returnreturnValue.booleanValue();
}

//取消配对
(ClassbtClass,
BluetoothDevicedevice)

throwsException
{
MethodcreateBondMethod=btClass.getMethod("cancelBondProcess");
BooleanreturnValue=(Boolean)createBondMethod.invoke(device);
returnreturnValue.booleanValue();
}

/**
*
*@paramclsShow
*/
(ClassclsShow)
{
try
{
//取得所有方法
Method[]hideMethod=clsShow.getMethods();
inti=0;
for(;i<hideMethod.length;i++)
{
Log.e("methodname",hideMethod[i].getName()+";andtheiis:"
+i);
}
//取得所有常量
Field[]allFields=clsShow.getFields();
for(i=0;i<allFields.length;i++)
{
Log.e("Fieldname",allFields[i].getName());
}
}
catch(SecurityExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(IllegalArgumentExceptione)
{
//thrownewRuntimeException(e.getMessage());
e.printStackTrace();
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}</b>
执行时直接使用:
<b>publicstaticbooleanpair(StringstrAddr,StringstrPsw)
{
booleanresult=false;
=BluetoothAdapter
.getDefaultAdapter();

bluetoothAdapter.cancelDiscovery();

if(!bluetoothAdapter.isEnabled())
{
bluetoothAdapter.enable();
}

if(!BluetoothAdapter.checkBluetoothAddress(strAddr))
{//检查蓝牙地址是否有效

Log.d("mylog","devAdneffient!");
}

BluetoothDevicedevice=bluetoothAdapter.getRemoteDevice(strAddr);

if(device.getBondState()!=BluetoothDevice.BOND_BONDED)
{
try
{
Log.d("mylog","NOTBOND_BONDED");
ClsUtils.setPin(device.getClass(),device,strPsw);//手机和蓝牙采集器配对
ClsUtils.createBond(device.getClass(),device);
remoteDevice=device;//配对完毕就把这个设备对象传给全局的remoteDevice
result=true;
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock

Log.d("mylog","setPiNfailed!");
e.printStackTrace();
}//

}
else
{
Log.d("mylog","HASBOND_BONDED");
try
{
ClsUtils.createBond(device.getClass(),device);
ClsUtils.setPin(device.getClass(),device,strPsw);//手机和蓝牙采集器配对
ClsUtils.createBond(device.getClass(),device);
remoteDevice=device;//如果绑定成功,就直接把这个设备对象传给全局的remoteDevice
result=true;
}
catch(Exceptione)
{
//TODOAuto-generatedcatchblock
Log.d("mylog","setPiNfailed!");
e.printStackTrace();
}
}
returnresult;
}</b>

G. 这段 安卓软件中的代码中如何判断 手机是否连接到 蓝牙设备

安卓蓝牙中。要连接其他设备要调用connect()函数。。此函数你可以去看一下,官方说的是:若连接成功,就不会产生异常。不成功,就会产生异常。。。所以看看程序会运行异常不。运行了,说明没有连接成功。没运行,则连接成功了。
try {
// 连接
connect(device);
} catch (IOException e) {
Toast.makeText(MainActivity.this, "没有成功连接设备", //1
Toast.LENGTH_SHORT).show();
}

若成功了,不会运行1,不成功,就会产生异常。运行异常中的程序,也就是代码1.

H. Android蓝牙怎么检测连接状态

首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限

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

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

然后,看下api,Android所有关于蓝牙开发的类都在android.bluetooth包下,如下图,只有8个类

而我们需要用到了就只有几个而已:

1.BluetoothAdapter 顾名思义,蓝牙适配器,直到我们建立bluetoothSocket连接之前,都要不断操作它BluetoothAdapter里的方法很多,常用的有以下几个:cancelDiscovery() 根据字面意思,是取消发现,也就是说当我们正在搜索设备的时候调用这个方法将不再继续搜索disable()关闭蓝牙enable()打开蓝牙,这个方法打开蓝牙不会弹出提示,更多的时候我们需要问下用户是否打开,一下这两行代码同样是打开蓝牙,不过会提示用户:Intemtenabler=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enabler,reCode);//同startActivity(enabler);

getAddress()获取本地蓝牙地址getDefaultAdapter()获取默认BluetoothAdapter,实际上,也只有这一种方法获取BluetoothAdaptergetName()获取本地蓝牙名称getRemoteDevice(String address)根据蓝牙地址获取远程蓝牙设备getState()获取本地蓝牙适配器当前状态(感觉可能调试的时候更需要)isDiscovering()判断当前是否正在查找设备,是返回true***isEnabled()判断蓝牙是否打开,已打开返回true,否则,返回false***(String name,UUID uuid)根据名称,UUID创建并返回BluetoothServerSocket,这是创建BluetoothSocket服务器端的第一步startDiscovery()开始搜索,这是搜索的第一步2.BluetoothDevice看名字就知道,这个类描述了一个蓝牙设备(UUIDuuid)根据UUID创建并返回一个BluetoothSocket这个方法也是我们获取BluetoothDevice的目的——创建BluetoothSocket

这个类其他的方法,如getAddress(),getName(),同BluetoothAdapter

3.BluetoothServerSocket如果去除了Bluetooth相信大家一定再熟悉不过了,既然是Socket,方法就应该都差不多,这个类一种只有三个方法

两个重载的accept(),accept(inttimeout)两者的区别在于后面的方法指定了过时时间,需要注意的是,执行这两个方法的时候,直到接收到了客户端的请求(或是过期之后),都会阻塞线程,应该放在新线程里运行!

还有一点需要注意的是,这两个方法都返回一个BluetoothSocket,最后的连接也是服务器端与客户端的两个BluetoothSocket的连接

close()这个就不用说了吧,翻译一下——关闭!4.BluetoothSocket,跟BluetoothServerSocket相对,是客户端一共5个方法,不出意外,都会用到

close(),关闭connect()连接getInptuStream()获取输入流getOutputStream()获取输出流getRemoteDevice()获取远程设备,这里指的是获取bluetoothSocket指定连接的那个远程蓝牙设备

I. android怎么来判断蓝牙开、关的状态求代码

Android 蓝牙编程的基本步骤:

  1. 获取蓝牙适配器BluetoothAdapterblueadapter=BluetoothAdapter.getDefaultAdapter();

  2. 如果BluetoothAdapter 为null,说明android手机没有蓝牙模块。

    判断蓝牙模块是否开启,blueadapter.isEnabled() true表示已经开启,false表示蓝牙并没启用。

  3. 启动配置蓝牙可见模式,即进入可配对模式Intentin=newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

    in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,200);

    startActivity(in); ,200就表示200秒。

  4. 获取蓝牙适配器中已经配对的设备Set<BluetoothDevice>device=blueadapter.getBondedDevices();

  5. 还需要在androidManifest.xml中声明蓝牙的权限

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

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


接下来就是根据自己的需求对BluetoothAdapter的操作了。

J. syu android蓝牙连接方法

syu android蓝牙连接方法先展示代码结构。

蓝牙是一种无线数据与语音通信的开放性全球规范,它以低成本的短距离无线连接为基础,可为固定的或移动的终端设备提供廉价的接入服务。

蓝牙(Bluetooth)是一项短途无线电连接系统,它可以将不同的电子器材连系起来。原理就好像收音机一样,装有蓝牙的电子器材,可以接收外来的讯息,从而进行特定的指令。

蓝牙简介:

不过,蓝牙不但可以接收,也都可以“传送”,因此装有蓝牙的电子器材,能够互相沟通。现在,大部分的电脑配件,如打印机、荧幕等,都要接驳上电线,才可以互传讯息,但蓝牙透过其短途的接收系统,便可以使这些配件在没有驳线下,仍然能够传送指令,做到真正“无线”的世界。

头蓝技术实质内容是为固定设备或移动设备之间的通信环境建立通用的近距无线接口,将通信技术与计算机技术进一步结合起来,使各种设备在没有电线或电缆相互连接的情况下,能在近距离范围内实现相互通信或操作。蓝牙功能可以参考下面的操作打开使用,打开其他设备的蓝牙。

并使其对其他设备可见,打开下拉顶帘,点击蓝牙图标使其变为绿色,跳出提示框,勾选对其他设备可见。点击扫描,搜索到其他设备后,点击该设备名称,双方点确定后配对成功。选择要传输的文件,共享通过蓝牙即可传输文件。

热点内容
山东电脑服务器租用云主机 发布:2024-06-19 02:46:29 浏览:842
正宗安卓手机哪个好 发布:2024-06-19 02:38:35 浏览:202
dynamo采用什么方式存储数据 发布:2024-06-19 02:28:09 浏览:377
好玩的服务器我的世界电脑版地址 发布:2024-06-19 02:27:47 浏览:594
安卓平板没有通用功能怎么办 发布:2024-06-19 02:15:33 浏览:372
iisphp性能 发布:2024-06-19 01:42:52 浏览:736
服务器较差什么意思 发布:2024-06-19 01:17:25 浏览:579
用英文讲解ppt的脚本 发布:2024-06-19 01:12:54 浏览:495
录音机存储在什么地方 发布:2024-06-19 01:12:09 浏览:491
c语言c40 发布:2024-06-19 01:11:20 浏览:52