Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- private BluetoothAdapter BA;
- public final static String MODULE_MAC = "00:21:13:00:14:E4";
- private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
- private BluetoothSocket mmSocket;
- private BluetoothDevice mmDevice;
- public InputStream IStream;
- public OutputStream OStream;
- public FirebaseDatabase database = FirebaseDatabase.getInstance();
- public DatabaseReference myRef = database.getReference("RCCarNew/CurrentCommand");
- public DatabaseReference myRef2 = database.getReference("RCCarNew/ConnectStat");
- public DatabaseReference myRef3 = database.getReference("RCCarNew/FlashStatus");
- public boolean first1 = false;
- private static final int PERMISSION_REQ_ID = 22;
- private static final String[] REQUESTED_PERMISSIONS = {Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA};
- private RtcEngine mRtcEngine;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- BA = BluetoothAdapter.getDefaultAdapter();
- checkSelfPermission(REQUESTED_PERMISSIONS[0], PERMISSION_REQ_ID);
- checkSelfPermission(REQUESTED_PERMISSIONS[1], PERMISSION_REQ_ID);
- initAgoraEngine();
- setupSession();
- mRtcEngine.joinChannel(null, "test-channel", "Extra Optional Data", 0);
- mRtcEngine.setupLocalVideo(new VideoCanvas(findViewById(R.id.surfaceView2), VideoCanvas.RENDER_MODE_FIT, 0));
- Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(turnOn, 0);
- BluetoothDevice device = BA.getRemoteDevice(MODULE_MAC);
- BluetoothSocket tmp = null;
- mmDevice = device;
- try {
- tmp = mmDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);
- } catch (IOException e) {
- e.printStackTrace();
- System.out.print("ERROR!! java.io.IOEXCEPTION");
- }
- mmSocket = tmp;
- try {
- assert mmSocket != null;
- mmSocket.connect();
- IStream = mmSocket.getInputStream();
- OStream = mmSocket.getOutputStream();
- } catch (IOException e) {
- e.printStackTrace();
- System.out.print("ERROR!! java.io.IOEXCEPTION during connecting");
- }
- myRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- if (dataSnapshot.getValue() != null && first1){
- try {
- OStream.write((dataSnapshot.getValue()).toString().getBytes());
- }catch (IOException e) {
- e.printStackTrace();
- }
- }else{
- System.out.print("hmmn, send a mesage you big idiot");
- first1 = true;
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- myRef2.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- BluetoothDevice device = BA.getRemoteDevice(MODULE_MAC);
- BluetoothSocket tmp = null;
- mmDevice = device;
- if (dataSnapshot.getValue() == null){
- System.out.print("GIVE ME DATA TO PROCESS YOU IDIOT");
- }
- else{
- if (dataSnapshot.getValue() == "Y") {
- try {
- tmp = mmDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);
- } catch (IOException e) {
- e.printStackTrace();
- System.out.print("ERROR!! java.io.IOEXCEPTION");
- }
- mmSocket = tmp;
- try {
- assert mmSocket != null;
- mmSocket.connect();
- IStream = mmSocket.getInputStream();
- OStream = mmSocket.getOutputStream();
- } catch (IOException e) {
- e.printStackTrace();
- System.out.print("ERROR!! java.io.IOEXCEPTION during connecting");
- }
- }
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- myRef3.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- if (dataSnapshot.getValue() == "O"){
- try {
- OStream.write("J".getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }else{
- try {
- OStream.write("M".getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- }
- public void checkSelfPermission(String permission, int requestCode) {
- if (ContextCompat.checkSelfPermission(this,
- permission)
- != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(this,
- REQUESTED_PERMISSIONS,
- requestCode);
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode,
- @NonNull String[] permissions, @NonNull int[] grantResults) {
- if (requestCode == PERMISSION_REQ_ID) {
- if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
- return;
- }
- // if permission granted, initialize the engine
- initAgoraEngine();
- }
- }
- private void initAgoraEngine() {
- try {
- mRtcEngine = RtcEngine.create(getBaseContext(), "944e894435fb4083a3cfc90066509245", mRtcEventHandler);
- } catch (Exception e) {
- throw new RuntimeException("NEED TO check rtc sdk init fatal error\n" + Log.getStackTraceString(e));
- }
- setupSession();
- }
- private void setupSession() {
- mRtcEngine.setChannelProfile(Constants.CHANNEL_PROFILE_COMMUNICATION);
- mRtcEngine.enableVideo();
- mRtcEngine.setVideoEncoderConfiguration(new VideoEncoderConfiguration(VideoEncoderConfiguration.VD_840x480, VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_30,
- VideoEncoderConfiguration.STANDARD_BITRATE,
- VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_FIXED_LANDSCAPE));
- }
- private final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {
- @Override
- public void onFirstRemoteVideoFrame(int uid, int width, int height, int elapsed) {
- super.onFirstRemoteVideoFrame(uid, width, height, elapsed);
- mRtcEngine.setupRemoteVideo(new VideoCanvas(findViewById(R.id.surfaceView), VideoCanvas.RENDER_MODE_FIT, uid));
- }
- };
- }
Add Comment
Please, Sign In to add comment