我想在第二次打开应用程序时自动连接到MI Band 2

时间:2019-02-03 12:50:51

标签: android bluetooth

问题在于,每次打开我的应用程序时,我想从MI Band 2中获取数据,就必须触摸MI Band 2的按钮以授予配对的权限。我想触摸按钮仅在我第一次打开我的应用程序时才授予权限,而下次我打开我的应用程序时,MI Band 2会自动连接。我给您下面MainActivity的代码。

    public class MainActivity extends AppCompatActivity implements BTMIBand2Helper.BLEAction {

    public String macSelectedDevice;
    Button btn_test,btn_test2,btn_test3;
    Handler handler = new Handler(Looper.getMainLooper());
    BTMIBand2Helper bthelper = null;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
    = new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    Fragment fragment = null;
    int i = item.getItemId();

    if (i == R.id.navigation_home) {
        fragment = new MIPulsera().newInstance();
    } else if (i == R.id.navigation_dashboard) {
        fragment = new Explorar().newInstance();
    } else if (i == R.id.navigation_notifications) {
        fragment = new Ajustes().newInstance();
    }

    if (fragment != null) {
        replaceFragment(fragment);
        return true;
    }

    return false;
}
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
                                navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

btn_test = (Button) findViewById(R.id.btn_test);
btn_test2 = (Button) findViewById(R.id.btn_test2);
btn_test3 = (Button) findViewById(R.id.btn_test3);

Bundle extras = getIntent().getExtras();
if (extras != null) {
    macSelectedDevice = extras.getString("MAC");
}

firstReplaceFragment();

bthelper = new BTMIBand2Helper(MainActivity.this, handler);
bthelper.addListener(this);
bthelper.connect(macSelectedDevice);

btn_test.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        bthelper.RequestAuthentication();
    }
});
btn_test2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        bthelper.SendTextNotification("Izquierda");
    }
});

btn_test3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        bthelper.SendTextNotification("Derecha");
    }
});

    }

    private void firstReplaceFragment() {
android.support.v4.app.FragmentTransaction fragmentTransaction =         getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout,new MIPulsera().newInstance());
fragmentTransaction.commit();
    }

    private void replaceFragment(android.support.v4.app.Fragment fragment) {
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_layout,fragment);
fragmentTransaction.commit();
    }

    @Override

    public void onNotification(BluetoothGatt bluetoothGatt,         BluetoothGattCharacteristic characteristic){

UUID alertUUID = characteristic.getUuid();

if(alertUUID.equals(MIBand2Constants.UUID_BUTTON)){
    handler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, "Touch Button", Toast.LENGTH_LONG).show();
        }
    });
}

    }

0 个答案:

没有答案