[uiautomator篇] bluetooth---接口来做
package com.softwinner.performance.frameratetest; import android.Manifest; 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.content.pm.PackageManager; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.Toast; import java.util.Set; public class MainActivity extends AppCompatActivity { private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); private String mLogTag = "bluetoothtest"; 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 remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.i(mLogTag,remoteDevice.getName() + " address: " + remoteDevice.getAddress()); } else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ Log.i(mLogTag,"search finished"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.KILL_BACKGROUND_PROCESSES) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.KILL_BACKGROUND_PROCESSES},1); } if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1); } if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1); } if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},10); } searchDevices(); } private void searchDevices(){ if(mBluetoothAdapter == null){ Log.i(mLogTag,"device not support bluetooth"); Toast.makeText(MainActivity.this,"device not support bluetooth",Toast.LENGTH_SHORT).show(); return; } if(!mBluetoothAdapter.isEnabled()){ mBluetoothAdapter.enable(); mBluetoothAdapter.cancelDiscovery(); Toast.makeText(MainActivity.this,"bluetooth has open",Toast.LENGTH_SHORT).show(); } IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); filter.setPriority(Integer.MAX_VALUE); registerReceiver(mReceiver,filter); mBluetoothAdapter.startDiscovery(); try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();} // showBoundDevices(); } private void showBoundDevices(){ // List<Map<String, String>> mBoundDevicesList = new ArrayList<>(); Set<BluetoothDevice> pairDevices = mBluetoothAdapter.getBondedDevices(); if(pairDevices.size() > 0){ for(BluetoothDevice boundDevice : pairDevices){ System.out.println(boundDevice.getName() + " " + boundDevice.getAddress()); Log.i(mLogTag, boundDevice.getName()+ " " + boundDevice.getAddress()); } } } }
主要的是
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.softwinner.performance.frameratetest"> <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="com.softwinner.performance.frameratetest.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>