| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 | 
							- /*
 
-  * Copyright (C) 2013 The Android Open Source Project
 
-  *
 
-  * Licensed under the Apache License, Version 2.0 (the "License");
 
-  * you may not use this file except in compliance with the License.
 
-  * You may obtain a copy of the License at
 
-  *
 
-  *      http://www.apache.org/licenses/LICENSE-2.0
 
-  *
 
-  * Unless required by applicable law or agreed to in writing, software
 
-  * distributed under the License is distributed on an "AS IS" BASIS,
 
-  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
-  * See the License for the specific language governing permissions and
 
-  * limitations under the License.
 
-  */
 
- package com.slam.bboxble;
 
- import android.Manifest;
 
- import android.app.Activity;
 
- import android.bluetooth.BluetoothAdapter;
 
- import android.bluetooth.BluetoothDevice;
 
- import android.bluetooth.BluetoothManager;
 
- import android.content.Context;
 
- import android.content.Intent;
 
- import android.content.pm.PackageManager;
 
- import android.os.Build;
 
- import android.os.Bundle;
 
- import android.support.annotation.NonNull;
 
- import android.support.v4.app.ActivityCompat;
 
- import android.support.v4.content.ContextCompat;
 
- import android.util.Log;
 
- import android.view.LayoutInflater;
 
- import android.view.Menu;
 
- import android.view.MenuItem;
 
- import android.view.View;
 
- import android.view.ViewGroup;
 
- import android.widget.AdapterView;
 
- import android.widget.BaseAdapter;
 
- import android.widget.Button;
 
- import android.widget.ListView;
 
- import android.widget.TextView;
 
- import android.widget.Toast;
 
- import com.slam.bboxblelib.DeviceEvent;
 
- import com.slam.bboxblelib.MainBluetooth;
 
- import com.slam.bboxblelib.impl.MainBluetoothImpl;
 
- import com.slam.bboxblelib.Responser;
 
- import java.util.ArrayList;
 
- import java.util.Set;
 
- /**
 
-  * Activity for scanning and displaying available Bluetooth LE devices.
 
-  */
 
- public class DeviceScanActivity extends Activity implements View.OnClickListener {
 
-     private final String TAG = "DeviceScanActivity";
 
-     private LeDeviceListAdapter mLeDeviceListAdapter;
 
-     private LeDeviceListAdapter mPairLeDeviceListAdapter;
 
-     private BluetoothAdapter mBluetoothAdapter;
 
-     private boolean mScanning;
 
-     private ListView mListView, mPairListView;
 
-     private Button mButtonStopScan, mButtonStartScan;
 
-     private TextView mTextViewResult;
 
-     private boolean noNeedShowDialog = false;
 
-     private static final int REQUEST_ENABLE_BT = 1;
 
-     private static final int REQUEST_WRITE_STORAGE = 112;
 
-     //蓝牙
 
-     private MainBluetoothImpl mainBluetooth = new MainBluetoothImpl();
 
-     @Override
 
-     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
 
-         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 
-         switch (requestCode) {
 
-             case REQUEST_WRITE_STORAGE:
 
-                 FileUnit.getOTAFilePath();
 
-                 break;
 
-             default:
 
-                 break;
 
-         }
 
-     }
 
-     @Override
 
-     public void onCreate(Bundle savedInstanceState) {
 
-         super.onCreate(savedInstanceState);
 
-         getActionBar().setTitle(R.string.title_devices);
 
-         setContentView(R.layout.list_device_view);
 
-         if (Build.VERSION.SDK_INT >= 23) {
 
-             int permission1 = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION);
 
-             int permission2 = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
 
-             if (permission1 != PackageManager.PERMISSION_GRANTED || permission2 != PackageManager.PERMISSION_GRANTED) {
 
-                 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
 
-                     ActivityCompat.requestPermissions(DeviceScanActivity.this,
 
-                             new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE},
 
-                             REQUEST_WRITE_STORAGE);
 
-                 } else {
 
-                     ActivityCompat.requestPermissions(DeviceScanActivity.this,
 
-                             new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE},
 
-                             REQUEST_WRITE_STORAGE);
 
-                 }
 
-             }
 
-         } else {
 
-             FileUnit.getOTAFilePath();
 
-         }
 
-         mButtonStartScan = (Button) findViewById(R.id.button_startScan);
 
-         mButtonStopScan = (Button) findViewById(R.id.button_stopScan);
 
-         mButtonStartScan.setOnClickListener(this);
 
-         mButtonStopScan.setOnClickListener(this);
 
-         mButtonStopScan.setVisibility(View.GONE);
 
-         mTextViewResult = (TextView) findViewById(R.id.textview_OTAResult);
 
-         mListView = (ListView) findViewById(R.id.list_device);
 
-         mPairListView = (ListView) findViewById(R.id.list_pair_device);
 
-         mPairListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 
-             @Override
 
-             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 
-                 final BluetoothDevice device = mPairLeDeviceListAdapter.getDevice(position);
 
-                 if (device == null) return;
 
-                 final Intent intent = new Intent(DeviceScanActivity.this, DeviceControlActivity.class);
 
-                 intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
 
-                 intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
 
-                 mScanning = false;
 
-                 //蓝牙部分
 
-                 mainBluetooth.stopScanLeDevice();
 
-                 noNeedShowDialog = false;
 
-                 startActivityForResult(intent, 2);
 
-             }
 
-         });
 
-         mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 
-             @Override
 
-             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
 
-                 final BluetoothDevice device = mLeDeviceListAdapter.getDevice(i);
 
-                 if (device == null) return;
 
-                 final Intent intent = new Intent(DeviceScanActivity.this, DeviceControlActivity.class);
 
-                 intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
 
-                 intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
 
-                 mScanning = false;
 
-                 //蓝牙部分
 
-                 mainBluetooth.stopScanLeDevice();
 
-                 noNeedShowDialog = false;
 
-                 startActivityForResult(intent, 2);
 
-             }
 
-         });
 
-         // Use this check to determine whether BLE is supported on the device.  Then you can
 
-         // selectively disable BLE-related features.
 
-         if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
 
-             Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
 
-             finish();
 
-         }
 
-         // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
 
-         // BluetoothAdapter through BluetoothManager.
 
-         final BluetoothManager bluetoothManager =
 
-                 (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
 
-         mBluetoothAdapter = bluetoothManager.getAdapter();
 
-         //初始化蓝牙参数部分
 
-         Responser responser = new Responser() {
 
-             @Override
 
-             public void onSuccess(DeviceEvent deviceEvent) {
 
-                 if (deviceEvent.getType().equals("pairedDevices")) {
 
-                     if (deviceEvent.getDevices().size() > 0) {
 
-                         for (BluetoothDevice device : deviceEvent.getDevices()) {
 
-                             Log.e(TAG, "**********DeviceEvent responser=" + device.getAddress() + device.getName());
 
-                             mPairLeDeviceListAdapter.addDevice(device);
 
-                             mPairLeDeviceListAdapter.notifyDataSetChanged();
 
-                         }
 
-                         mScanning = true;
 
-                     }
 
-                 } else if (deviceEvent.getType().equals("device")) {
 
- //                        Log.e(TAG, "**********DeviceEvent responser=" + deviceEvent.getDevice().getAddress());
 
- //                        Log.e(TAG, "**********DeviceEvent responser=" + deviceEvent.getRssi());
 
-                     if (deviceEvent.getDevice().getBondState() != BluetoothDevice.BOND_BONDED) {
 
-                         mLeDeviceListAdapter.addDevice(deviceEvent.getDevice());
 
-                         mLeDeviceListAdapter.notifyDataSetChanged();
 
-                     }
 
-                     ;
 
-                 } else if (deviceEvent.getType().equals("none")) {
 
-                     runOnUiThread(new Runnable() {
 
-                         @Override
 
-                         public void run() {
 
-                             mButtonStopScan.performClick();
 
-                         }
 
-                     });
 
-                 }
 
-             }
 
-             @Override
 
-             public void onFailed(String prompt) {
 
-             }
 
- //                    @Override
 
- //                    public void onSuccessDevices(Set<BluetoothDevice> devices) {
 
- //                        if (devices.size() > 0) {
 
- //                            for (BluetoothDevice device : devices) {
 
- //                                Log.e(TAG, "**********DeviceEvent responser=" +device.getAddress() + device.getName() );
 
- //                                mPairLeDeviceListAdapter.addDevice(device);
 
- //                                mPairLeDeviceListAdapter.notifyDataSetChanged();
 
- //                            }
 
- //                            mScanning = true;
 
- //                        }
 
- //
 
- //                    }
 
- //
 
- //                    @Override
 
- //                    public void onSuccess(String data) {
 
- //                        runOnUiThread(new Runnable() {
 
- //                            @Override
 
- //                            public void run() {
 
- //                                mButtonStopScan.performClick();
 
- //                            }
 
- //                        });
 
- //                    }
 
-         };
 
-         mainBluetooth.onCreate(this,responser);
 
-         // Checks if Bluetooth is supported on the device.
 
-         if (mBluetoothAdapter == null) {
 
-             Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
 
-             finish();
 
-             return;
 
-         }
 
-     }
 
-     @Override
 
-     protected void onStart() {
 
-         super.onStart();
 
-     }
 
-     @Override
 
-     public boolean onCreateOptionsMenu(Menu menu) {
 
-         getMenuInflater().inflate(R.menu.main, menu);
 
-         if (!mScanning) {
 
-             menu.findItem(R.id.menu_stop).setVisible(false);
 
-             menu.findItem(R.id.menu_scan).setVisible(true);
 
-             menu.findItem(R.id.menu_refresh).setActionView(null);
 
-             mButtonStopScan.setVisibility(View.GONE);
 
-             mButtonStartScan.setVisibility(View.VISIBLE);
 
-         } else {
 
-             menu.findItem(R.id.menu_stop).setVisible(true);
 
-             menu.findItem(R.id.menu_scan).setVisible(false);
 
-             menu.findItem(R.id.menu_refresh).setActionView(
 
-                     R.layout.actionbar_indeterminate_progress);
 
-             mButtonStopScan.setVisibility(View.VISIBLE);
 
-             mButtonStartScan.setVisibility(View.GONE);
 
-         }
 
-         return true;
 
-     }
 
-     @Override
 
-     public boolean onOptionsItemSelected(MenuItem item) {
 
-         return true;
 
-     }
 
-     @Override
 
-     protected void onResume() {
 
-         super.onResume();
 
-         // Ensures Bluetooth is enabled on the device.  If Bluetooth is not currently enabled,
 
-         // fire an intent to display a dialog asking the user to grant permission to enable it.
 
-         Log.e(TAG, "onResume");
 
-         if (!noNeedShowDialog) {
 
-             if (!mBluetoothAdapter.isEnabled()) {
 
-                 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 
-                 startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
 
-             }
 
-         }
 
-         mLeDeviceListAdapter = new LeDeviceListAdapter();
 
-         mPairLeDeviceListAdapter = new LeDeviceListAdapter();
 
-         mListView.setAdapter(mLeDeviceListAdapter);
 
-         mPairListView.setAdapter(mPairLeDeviceListAdapter);
 
-     }
 
-     @Override
 
-     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 
-         super.onActivityResult(requestCode, resultCode, data);
 
-         // User chose not to enable Bluetooth.
 
-         Log.e(TAG, "back check resultCode: " + resultCode);
 
-         if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
 
-             finish();
 
-             return;
 
-         } else if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_OK) {
 
-             Toast.makeText(this, "Bluetooth Enable", Toast.LENGTH_SHORT).show();
 
-         } else if (requestCode == 2) {
 
-             if (resultCode == 4 || resultCode == 3) {
 
-                 noNeedShowDialog = true;
 
-                 String aaa = data.getExtras().getString("Result OTA");
 
-                 mTextViewResult.setText(aaa);
 
-             } else if (resultCode == 5) {
 
-                 noNeedShowDialog = false;
 
-                 Toast.makeText(this, "OTA Done", Toast.LENGTH_SHORT).show();
 
-                 mTextViewResult.setText("");
 
-             } else {
 
-                 noNeedShowDialog = false;
 
-                 mTextViewResult.setText("");
 
-             }
 
-         }
 
-     }
 
-     @Override
 
-     protected void onPause() {
 
-         super.onPause();
 
-         if (mButtonStartScan.getVisibility() == View.GONE) {
 
-             mButtonStopScan.performClick();
 
-         }
 
-         mLeDeviceListAdapter.clear();
 
-         mPairLeDeviceListAdapter.clear();
 
-     }
 
-     @Override
 
-     protected void onDestroy() {
 
-         super.onDestroy();
 
-     }
 
-     @Override
 
-     public void onClick(View view) {
 
-         switch (view.getId()) {
 
-             case R.id.button_startScan:
 
-                 mLeDeviceListAdapter.clear();
 
-                 mPairLeDeviceListAdapter.clear();
 
-                 mButtonStopScan.setVisibility(View.VISIBLE);
 
-                 mButtonStartScan.setVisibility(View.GONE);
 
-                 mainBluetooth.startScanLeDevice();
 
-                 invalidateOptionsMenu();
 
-                 break;
 
-             case R.id.button_stopScan:
 
-                 mButtonStopScan.setVisibility(View.GONE);
 
-                 mButtonStartScan.setVisibility(View.VISIBLE);
 
-                 mainBluetooth.stopScanLeDevice();
 
-                 break;
 
-             default:
 
-                 break;
 
-         }
 
-     }
 
-     // Adapter for holding devices found through scanning.
 
-     private class LeDeviceListAdapter extends BaseAdapter {
 
-         private ArrayList<BluetoothDevice> mLeDevices;
 
-         private LayoutInflater mInflator;
 
-         public LeDeviceListAdapter() {
 
-             super();
 
-             mLeDevices = new ArrayList<BluetoothDevice>();
 
-             mInflator = DeviceScanActivity.this.getLayoutInflater();
 
-         }
 
-         public void addDevice(BluetoothDevice device) {
 
-             if (!mLeDevices.contains(device)) {
 
-                 mLeDevices.add(device);
 
-             }
 
-         }
 
-         public BluetoothDevice getDevice(int position) {
 
-             if (mLeDevices.size() > position) {
 
-                 return mLeDevices.get(position);
 
-             } else {
 
-                 return null;
 
-             }
 
-         }
 
-         public void clear() {
 
-             mLeDevices.clear();
 
-         }
 
-         @Override
 
-         public int getCount() {
 
-             return mLeDevices.size();
 
-         }
 
-         @Override
 
-         public Object getItem(int i) {
 
-             return mLeDevices.get(i);
 
-         }
 
-         @Override
 
-         public long getItemId(int i) {
 
-             return i;
 
-         }
 
-         @Override
 
-         public View getView(int i, View view, ViewGroup viewGroup) {
 
-             ViewHolder viewHolder;
 
-             // General ListView optimization code.
 
-             if (view == null) {
 
-                 view = mInflator.inflate(R.layout.listitem_device, null);
 
-                 viewHolder = new ViewHolder();
 
-                 viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
 
-                 viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
 
-                 view.setTag(viewHolder);
 
-             } else {
 
-                 viewHolder = (ViewHolder) view.getTag();
 
-             }
 
-             BluetoothDevice device = mLeDevices.get(i);
 
-             final String deviceName = device.getName();
 
-             if (deviceName != null && deviceName.length() > 0)
 
-                 viewHolder.deviceName.setText(deviceName);
 
-             else
 
-                 viewHolder.deviceName.setText(R.string.unknown_device);
 
-             viewHolder.deviceAddress.setText(device.getAddress());
 
-             return view;
 
-         }
 
-     }
 
-     @Override
 
-     protected void onSaveInstanceState(Bundle outState) {
 
-         super.onSaveInstanceState(outState);
 
-         outState.putBoolean("HBG", noNeedShowDialog);
 
-     }
 
-     @Override
 
-     protected void onRestoreInstanceState(Bundle savedInstanceState) {
 
-         super.onRestoreInstanceState(savedInstanceState);
 
-         noNeedShowDialog = savedInstanceState.getBoolean("HBG");
 
-     }
 
-     static class ViewHolder {
 
-         TextView deviceName;
 
-         TextView deviceAddress;
 
-     }
 
- }
 
 
  |