Giter VIP home page Giter VIP logo

bluetooth's Introduction

Bluetooth

BLE设备蓝牙连接模块化代码示例

启动方法:BluetoothLeService.getInstance(Context).startAlarmService();

1、AndroidMainifest.xml权限:

      <!--app只支持拥有BLE的设备上运行-->
<uses-feature
        android:name="android.hardware.bluetooth_le"
        android:required="true" />

2、在代码中动态注册:

注意:6.0以上需要添加运行时权限

    /**
 * 蓝牙申请权限
 */
private void checkBluetoothPermission() {
    if (Build.VERSION.SDK_INT >= 23) {
        //校验是否已具有模糊定位权限
        if (ContextCompat.checkSelfPermission(TYMposActivity.this,
                Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(TYMposActivity.this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
        } else {
            //具有权限
            connectBluetooth();
        }
    } else {
        //系统不高于6.0直接执行
        connectBluetooth();
    }
}

/**
 * 申请权限结果
 *
 * @param requestCode
 * @param permissions
 * @param grantResults
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    doNext(requestCode, grantResults);
}

private void doNext(int requestCode, int[] grantResults) {
    if (requestCode == MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //同意权限
            connectBluetooth();
        } else {
            // 权限拒绝
            // 下面的方法最好写一个跳转,可以直接跳转到权限设置页面,方便用户
            denyPermission();
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.