これが私のアプリです:Bluetoothスキャンを実行し、スキャン関数はデバイスを見つけるとコールバックします。特定のデバイス(デバイス名で認識される)が見つかったら、特定のボタンのテキストを「検索中」から「接続」に変更したいと思います。
ただし、コールバックのスコープ内でボタンに到達することはできません。
これを行う方法はありますか?これは純粋にスコープの問題だと思います。私はこの種のことについてほとんど経験がありません。
コード:
Context context;
context = this;
update_str = "";
Button ConnectButton = (Button) findViewById(R.id.Connect);
ConnectButton.setText("Waiting for device to be found");
Button ScanButton = (Button) findViewById(R.id.Scan);
ScanButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btAdapter.startLeScan(leScanCallback);
}
});
BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
update_str = update_str.concat(" " + device.getName());
((TextView)findViewById (R.id.text)).setText (update_str);
nom_device = device.getName();
if (nom_device=="bill_gates"){
context.ConnectButton.setText(nom_device);
context.ConnectButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, context.btleGattCallback);
}
});
}
}
};
コンパイラエラーログ:
C:\Code\Grizz\app\src\main\java\com\grizz\grizzmvp\MainActivity.java
Error:(84, 28) error: cannot find symbol variable ConnectButton
Error:(89, 117) error: cannot find symbol variable btleGattCallback
Error:(86, 28) error: cannot find symbol variable ConnectButton
Note: C:\Code\Grizz\app\src\main\java\com\grizz\grizzmvp\MainActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 1.381 secs
Information:4 errors
Information:0 warnings
Information:See complete output in console
匿名の内部クラスがメソッドの本体内で定義されている場合、そのメソッドのスコープでfinalと宣言されたすべての変数は、内部クラス内からアクセスできます。スカラー値の場合、一度割り当てられると、最終変数の値は変更できません。オブジェクト値の場合、参照は変更できません。これにより、Javaコンパイラは実行時に変数の値を「キャプチャ」し、コピーをフィールドとして内部クラスに格納できます。外部メソッドが終了し、そのスタックフレームが削除されると、元の変数は失われますが、内部クラスのプライベートコピーはクラス自体のメモリに保持されます。
final Button ConnectButton = (Button) findViewById(R.id.Connect);
ConnectButton.setText("Waiting for device to be found");
final Button ScanButton = (Button) findViewById(R.id.Scan);
ScanButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btAdapter.startLeScan(leScanCallback);
}
});
BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
update_str = update_str.concat(" " + device.getName());
((TextView)findViewById (R.id.text)).setText (update_str);
nom_device = device.getName();
if (nom_device.equals("bill_gates")){
ConnectButton.setText(nom_device);
ConnectButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, context.btleGattCallback);
}
});
}
}
};
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加