① syu android藍牙連接方法
syu android藍牙連接方法:打開其他設備的藍牙,並使其對其他設備可見。打開下拉頂簾,點擊藍牙圖標使其變為綠色,跳出提示框,勾選對其他設備可見。
syu android判斷藍牙模塊是否開啟,blueadapter.isEnabled() true表示已經開啟,false表示藍牙並沒啟用。
syu android啟動配置藍牙可見模式,即進入可配對模式Intent in=newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)。
藍牙連接技術優勢:
支持語音和數據傳輸;採用無線電技術,傳輸范圍大,可穿透不同物質以及在物質間擴散;採用跳頻展頻技術,抗干擾性強,不易竊聽;使用在各國都不受限制的頻譜,理論上說,不存在干擾問題;功耗低;成本低。藍牙的劣勢:傳輸速度慢。
藍牙的技術性能參數:有效傳輸距離為10cm~10m,增加發射功率可達到100米,甚至更遠。收發器工作頻率為2.45GHz ,覆蓋范圍是相隔1MHz的79個通道(從2.402GHz到2.480GHz )。
② 如何實現android藍牙自動配對連接
android藍牙自動配對連接的具體代碼如下:
1. 獲取藍牙適配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();
如果BluetoothAdapter 為null,說明android手機沒有藍牙模塊。
2. 判斷藍牙模塊是否開啟,blueadapter.isEnabled() true表示已經開啟,false表示藍牙並沒啟用。
3. 啟動配置藍牙可見模式,即進入可配對模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
startActivity(in); ,200就表示200秒。
4. 獲取藍牙適配器中已經配對的設備Set<BluetoothDevice> device=blueadapter.getBondedDevices();
當然,還需要在androidManifest.xml中聲明藍牙的許可權
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
5.自動配對設置Pin值
static public boolean autoBond(Class btClass, BluetoothDevice device, String strPin)
throws Exception {
Method autoBondMethod = btClass.getMethod("setPin", new Class[] { byte[].class });
Boolean result = (Boolean) autoBondMethod
.invoke(device, new Object[] { strPin.getBytes() });
return result;
}
6.開始配對請求
static public boolean createBond(Class btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(device);
return returnValue.booleanValue();
}
③ android如何實現一台手機通過藍牙連另一台
手機之間通過藍牙連接傳送文件,請參考以下步驟:
1、雙方手機開啟藍牙開關,路徑:設置--常規--藍牙--開啟開關;
2、開啟開放檢測開關,開啟後才可以被附近所有藍牙設備檢測到;
3、掃描到需連接的藍牙設備,點擊連接;
4、雙方手機提示藍牙配對請求和配對型號,雙方手機點擊配對;
5、配對成功,已配對的設備裡面會顯示連接成功的藍牙設備;
6、打開文件,選擇藍牙發送和需發送到的藍牙設備,接收文件即可。
④ 如何實現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>
⑤ 如何實現android藍牙自動配對連接
android 2.35版本方法= 點擊設置,無線和網路,點擊藍牙設置,開啟藍牙,點擊你要配對的設備,叫您的朋友配對鏈接,就行了。
⑥ 如何實現android藍牙自動配對連接
android藍牙自動配對連接的具體代碼如下:
1. 獲取藍牙適配器BluetoothAdapter blueadapter=BluetoothAdapter.getDefaultAdapter();
如果BluetoothAdapter 為null,說明android手機沒有藍牙模塊。
2. 判斷藍牙模塊是否開啟,blueadapter.isEnabled() true表示已經開啟,false表示藍牙並沒啟用。
3. 啟動配置藍牙可見模式,即進入可配對模式Intent in=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
in.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200);
startActivity(in); ,200就表示200秒。
4. 獲取藍牙適配器中已經配對的設備Set<BluetoothDevice> device=blueadapter.getBondedDevices();
當然,還需要在androidManifest.xml中聲明藍牙的許可權
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
5.自動配對設置Pin值
static public boolean autoBond(Class btClass, BluetoothDevice device, String strPin)
throws Exception {
Method autoBondMethod = btClass.getMethod("setPin", new Class[] { byte[].class });
Boolean result = (Boolean) autoBondMethod
.invoke(device, new Object[] { strPin.getBytes() });
return result;
}
6.開始配對請求
static public boolean createBond(Class btClass, BluetoothDevice device) throws Exception {
Method createBondMethod = btClass.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(device);
return returnValue.booleanValue();
}
⑦ Android中如何實現藍牙的配對與連接
藍牙功能可以參考下面的操作打開使用:
1.打開其他設備的藍牙,並使其對其他設備可見。
2.打開下拉頂簾,點擊藍牙圖標使其變為綠色,跳出提示框,勾選對其他設備可見。
3.點擊掃描,搜索到其他設備後,點擊該設備名稱,雙方點確定後配對成功。
4.選擇要傳輸的文件,共享通過藍牙即可傳輸文件。