xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".MainActivity"> <button androID:ID="@+ID/btn" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="获取以配对的设备" /> <button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/bt_scan" androID:text="扫描设备" /></linearLayout>
androID java
package com.yifei.myapplication;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.nfc.Tag;import androID.support.annotation.Nullable;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;import java.util.Set;public class MainActivity extends AppCompatActivity { private static final int REQUEST_CODE = 10; private BluetoothAdapter mBluetoothAdapter; private static final String TAG = "MainActivity11"; private button pardevicebtn ; private button scanBtn; private final broadcastReceiver mReceiver = new broadcastReceiver() { @OverrIDe public voID onReceive(Context context, Intent intent) { String action = intent.getAction(); if(BluetoothDevice.ACTION_FOUND.equals(action)){ BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.d(TAG, device.getname()+"\n"+device.getAddress()); }else if(action.equals(BluetoothAdapter.ACTION_disCOVERY_FINISHED)){ Log.d(TAG, "discovery done"); } } }; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter(); if(mBluetoothAdapter==null){ Log.e(TAG, "Device not support Bluetooth"); }else { Toast.makeText(this,"设备支持蓝牙",Toast.LENGTH_LONG).show(); } pardevicebtn=findVIEwByID(R.ID.btn); scanBtn = findVIEwByID(R.ID.bt_scan); pardevicebtn.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { Set<BluetoothDevice> pairedDevices=mBluetoothAdapter.getBondedDevices(); for(BluetoothDevice device:pairedDevices){ Log.d(TAG, "Device name="+device.getname()+" address="+device.getAddress()); } } }); scanBtn.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { if(mBluetoothAdapter.isdiscovering()){ mBluetoothAdapter.canceldiscovery(); } mBluetoothAdapter.startdiscovery(); } }); IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_disCOVERY_FINISHED); registerReceiver(mReceiver,filter); } @OverrIDe protected voID onStart() { super.onStart(); if(!mBluetoothAdapter.isEnabled()){ Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent,REQUEST_CODE); }else { Toast.makeText(this,"蓝牙已经开启",Toast.LENGTH_LONG).show(); } IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver,filter); } @OverrIDe protected voID onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==REQUEST_CODE){ if(resultCode==RESulT_CANCELED){ Toast.makeText(this,"蓝牙已取消",Toast.LENGTH_LONG).show(); }else { Toast.makeText(this,"蓝牙已经开启",Toast.LENGTH_LONG).show(); } } } @OverrIDe protected voID onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver); }}总结
以上是内存溢出为你收集整理的简单的android 蓝牙发现附近的设备和以连接的设备全部内容,希望文章能够帮你解决简单的android 蓝牙发现附近的设备和以连接的设备所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)