Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Runnable update = new Runnable(){
- @Override
- public void run() {
- SearchBluetooth();
- mBluetoothAdapter.startDiscovery();
- stopCountDown();
- handler.postDelayed(this, 1000);//延遲時間
- }
- };
- private void checkBluetoothPermission() {
- if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) {
- // Android M Permission check
- if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},PERMISSION_REQUEST_COARSE_LOCATION);
- }
- }
- }
- public void SearchBluetooth(){
- if(mBluetoothAdapter == null){
- Toast.makeText(this,"not find the bluetooth",Toast.LENGTH_SHORT).show();
- finish();
- }
- if(!mBluetoothAdapter.isEnabled()){
- Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(intent,1);
- Set<BluetoothDevice> myDevices = mBluetoothAdapter.getBondedDevices();
- if(myDevices.size() > 0) {
- for(BluetoothDevice device : myDevices)
- bluetoothdeviceslist.add(device.getName()+":"+device.getAddress()+"\n");
- }
- }
- IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
- registerReceiver(myreceiver, filter);
- filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
- registerReceiver(myreceiver, filter);
- }
- public final BroadcastReceiver myreceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- //收到的廣播類型
- String action = intent.getAction();
- //發現設備的廣播
- if (BluetoothDevice.ACTION_FOUND.equals(action)) {
- //從intent中獲取設備
- BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
- int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
- double txPower = -59;
- double ratio = rssi*1.0/txPower;
- if (ratio < 1.0) {
- dis = Math.pow(ratio,10);
- }
- else {
- dis = (0.89976)*Math.pow(ratio,7.7095) + 0.111;
- }
- try{
- if(device.getName().equals(ID_target)) {
- TV1.setText(Double.toString(dis));
- }
- }
- catch(Exception e){
- }
- /*try{
- if(device.getName().equals(ID_target)) {
- TV1.setText(device.getName().toString() );
- tv2=Double.toString(dis);
- TV2.setText(Double.toString(dis));
- }
- }
- catch(Exception e){
- }*/
- }
- }
- };
- public void stopCountDown(){
- if(dis<1){
- timer.cancel();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement