kotran
726
2018-06-13 23:55:48
0
258

안드로이드에서 블루투스 4.0 검색이 되지않습니다.


다 되는 코드 만들어놨는데 뭔가 고치다보니 안되게되어버렸네요

아오 ㅜㅠ

startScan까지 잘가고, callback에서 자꾸 에러코드 2를 뿜뿜하는군요 ㅜㅠ


코드 전체는 아래와같습니다.


public class MainActivity extends AppCompatActivity {
    private BluetoothLeScanner mBLEScanner;
    BluetoothAdapter mBluetoothAdapter;
    private static final long SCAN_PERIOD = 5000;

    Handler mHandler=null;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();
        BluetoothAdapter bluetoothAdapter =  
        BluetoothAdapter.getDefaultAdapter();
        if(!bluetoothAdapter.isEnabled()){
            bluetoothAdapter.enable();

        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mBLEScanner=bluetoothAdapter.getBluetoothLeScanner();
        }
        mHandler = new Handler();
        btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("main","now calling bluetooth low energy");
                scanLeDevice(true);
            }
        });
    }

    void checkPermission(){
        String[] permissions = {
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,

        };
        int permissionCheck = PackageManager.PERMISSION_GRANTED;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]. 
            {Manifest.permission.READ_PHONE_STATE}, 0);
            requestPermissions(new String[]
            {Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
        }
        for (int i = 0; i < permissions.length; i++) {
            permissionCheck = ContextCompat.checkSelfPermission(this,      
            permissions[i]);

            if (permissionCheck == PackageManager.PERMISSION_DENIED) {
                Log.e("main", "denied" + permissions[i]);
                ActivityCompat.requestPermissions(this, permissions, i);
                if  
                (ActivityCompat.shouldShowRequestPermissionRationale(this, 
                permissions[i])) {
                    Log.e("main", "shouldshow" + permissions[i]);

                } else {
                    Log.e("main", "requesting" + permissions[i]);
                    ActivityCompat.requestPermissions(this, permissions, i);

                }
                break;
            } else {
                Log.e("main", "granted" + permissions[i]);

            }

        }


    }
    @TargetApi(21)
    private void scanLeDevice(final boolean enable) {
        if (enable) {
            //stops scanning after a pre-defined scan period

            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (Build.VERSION.SDK_INT < 21) {
                        System.out.println("BLE// mBluetoothAdapter.stopLeScan(mLeScanCallback) ");
                        mBluetoothAdapter.stopLeScan(mLeScanCallback);
                    } else {
                        mBLEScanner.stopScan(mScanCallback);
                        System.out.println("BLE// mLEScanner.stopScan(mScanCallback) ");
                    }
                }
            }, SCAN_PERIOD);

            if (Build.VERSION.SDK_INT < 21) {
                System.out.println("BLE// mBluetoothAdapter.startLeScan(mLeScanCallback)");
                mBluetoothAdapter.startLeScan(mLeScanCallback);
            } else {
                Log.e("main","starting scan for second"+mBLEScanner);

                mBLEScanner.startScan(mScanCallback);
            //mLEScanner.startScan(filters, settings, mScanCallback);
            }
        } else {
            if (Build.VERSION.SDK_INT < 21) {
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
            } else {
                mBLEScanner.stopScan(mScanCallback);
            }
        }
    }
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi,
                             byte[] scanRecord) {

        }
    };

    private ScanCallback mScanCallback = new ScanCallback() {

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Log.e("main","this name : "+result.getDevice().getName());
                Log.e("main","this addr : "+result.getDevice().getAddress());
            }
            BluetoothDevice btDevice = result.getDevice();
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
        }

        @Override
        public void onScanFailed(int errorCode) {
            Log.e("main", "Error Code: " + errorCode);
        }

    };
    }


여기는 로그 찍힌거입니다.

3:19:59.167 3251-3251/smartiot.ica.org.blelowtest E/main: grantedandroid.permission.INTERNET

06-13 23:19:59.167 3251-3251/smartiot.ica.org.blelowtest E/main: grantedandroid.permission.ACCESS_NETWORK_STATE

06-13 23:19:59.167 3251-3251/smartiot.ica.org.blelowtest E/main: grantedandroid.permission.BLUETOOTH_ADMIN

06-13 23:19:59.168 3251-3251/smartiot.ica.org.blelowtest E/main: grantedandroid.permission.BLUETOOTH

06-13 23:19:59.168 3251-3251/smartiot.ica.org.blelowtest E/main: deniedandroid.permission.ACCESS_COARSE_LOCATION

06-13 23:19:59.171 3251-3251/smartiot.ica.org.blelowtest E/main: requestingandroid.permission.ACCESS_COARSE_LOCATION

06-13 23:20:15.188 3251-3251/smartiot.ica.org.blelowtest E/main: now calling bluetooth low energy

06-13 23:20:15.189 3251-3251/smartiot.ica.org.blelowtest E/main: starting scan for secondandroid.bluetooth.le.BluetoothLeScanner@e24e0e4

06-13 23:20:15.264 3251-3251/smartiot.ica.org.blelowtest E/main: Error Code: 2


마지막으로 매니패스트.xml파일입니다.




<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="smartiot.ica.org.mbluetooth">

    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application

   ...




0
0
  • 답변 0

  • 로그인을 하시면 답변을 등록할 수 있습니다.