alexkendall / react-native-bluetooth-cross-platform

Cross-Platform Bluetooth & WiFi React Native Module. Powered by underdark http://underdark.io/.
179 stars 35 forks source link

Crash on Android 4.4 #11

Closed HZSamir closed 7 years ago

HZSamir commented 7 years ago

Hello again, Last week, I tried using your library on my old phone with Android 4.4, and it worked well. The messages were being sent back and forth between multiple devices, some on 5.0, the others on 6.0. So I put it away and continued working on something else.

Imagine my surprise this morning when I tried it again and the app would simply crash, My first though was to revert back to last week's commit, to no avail. What's more, even on a clean generated RN project, the app still crashes.

On a clean app, the code is like so:

import BluetoothCP from 'react-native-bluetooth-cross-platform';
// ...
        BluetoothCP.advertise();
        BluetoothCP.browse();

        BluetoothCP.addPeerDetectedListener(function(user) {
          BluetoothCP.inviteUser(user.id);        })

        BluetoothCP.addPeerLostListener(function(user) {
          console.log(`Lost a peer: ${user.id}`);
          BluetoothCP.disconnectFromPeer(user.id);
        });

        BluetoothCP.addInviteListener(function(user) {
          BluetoothCP.acceptInvitation(user.id);
        });

        BluetoothCP.addReceivedMessageListener(function(user) {
          //Parsing message
          console.log(user.message);
        })   

I haven't the faintest idea what could be causing this. DId you update the native library, causing this on older apis? Or is it something else? Thank you, and good day.

alexkendall commented 7 years ago

@Unforgiven-wanda Going to take a look at this. I believe I constructed the framework such that a user automatically disconnects from a peer if the peer is lost and so I think the error may be there from a quick glance, but the code shouldn't crash because of that. I'll take a deeper look tonight.

HZSamir commented 7 years ago

Hello, I totally forgot I had created this issue. The crash in fact originated from the fact that either addInviteListener or addPeerDetectedListener would sometimes be triggered with a null or undefined user argument. All I had to do was check the typeof first. But strangely enough, the crash would only occur on my 4.4 device. Adding the check solved it, but I'm not clear how. Closing this issue then. Thank you.